string uppercase!

The user enters three sentences and all of the letters are lowercase. Find the first character of each sentence and convert it into uppercase (use the function toupper )
********the problem with my code is that it changes ever letter to uppercase whereas i only need the first letter of each sentence to change and has no space between them. please help me out.*******

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 #include<iostream>
 #include<string>
 using namespace std;
 int main(){
        string a;
        cout<<"enter your sentence"<<endl;

        getline(cin,a);

         int s;
        s=a.length();
        cout<<s<<endl;

        for(int i=0;i<s;i++)
       if(islower(a[i])){
          a[i]=toupper(a[i]);
          cout<<a[i];
       }
    
  return 0;
}
I changed the code some, and I've found something that works.
(Note: People need to have some sense of grammar and not type a space as the first character, they have to have a space after the end of a sentence, etc.)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
#include<string>
using namespace std;
int main(){
    string a;
    cout<<"enter your sentence"<<endl;
    
    getline(cin,a);
    
    for (int i = 0; a[i]!=0; i++) {
        if (a[i-2]=='.' || i==0) {
            a[i]=toupper(a[i]);
        }
    }
    cout<<a<<endl;
    return 0;
}
Hi I have got one stupid question. Why you use a[i-2]? You can do it with that:
#include<iostream>
#include<string>
using namespace std;
int main()
{
char sentences[1000];
cout<<"Enter sentences with full stops"<<endl;
cin.getline(sentences, 1000);
for (int i=0; sentences[i]!='\0'; i++)
{
if ( (sentences[i-1]=='.') || (i==0) )
{
sentences[i]=toupper(sentences[i]);
}
}
cout<<sentences<<endl;
return 0;
}

Last edited on
Topic archived. No new replies allowed.