Answer:
The programming language is not stated; However, the program written in C++ is as follows: (See Attachment)
#include<iostream>
using namespace std;
string capt(string result)
{
result[0] = toupper(result[0]);
for(int i =0;i<result.length();i++){
if(result[i]=='.' || result[i]=='?' ||result[i]=='!') {
if(result[i+1]==' ') {
result[i+2] = toupper(result[i+2]);
}
if(result[i+2]==' ') {
result[i+3] = toupper(result[i+3]);
}
} }
return result;
}
int main(){
string sentence;
getline(cin,sentence);
cout<<capt(sentence);
return 0;
}
Explanation:
<em>The method to capitalize first letters of string starts here</em>
string capt(string result){
<em>This line capitalizes the first letter of the sentence</em>
result[0] = toupper(result[0]);
<em>This iteration iterates through each letter of the input sentence</em>
for(int i =0;i<result.length();i++){
This checks if the current character is a period (.), a question mark (?) or an exclamation mark (!)
if(result[i]=='.' || result[i]=='?' ||result[i]=='!') {
if(result[i+1]==' '){ ->This condition checks if the sentence is single spaced
result[i+2] = toupper(result[i+2]);
-> If both conditions are satisfied, a sentence is detected and the first letter is capitalized
}
if(result[i+2]==' '){ ->This condition checks if the sentence is double spaced
result[i+3] = toupper(result[i+3]);
-> If both conditions are satisfied, a sentence is detected and the first letter is capitalized
}
}
} <em>The iteration ends here</em>
return result; ->The new string is returned here.
}
The main method starts here
int main(){
This declares a string variable named sentence
string sentence;
This gets the user input
getline(cin,sentence);
This passes the input string to the method defined above
cout<<capt(sentence);
return 0;
}