How can i make the first letter of a sentence uppercase

I had to use a file to input a text with all capitol letters.
The pgm had to make all letters lowercase then change the first letter of each sentence into a capitol.

i read in the text using
string text
and changed the letters to lower case but now im having difficulty creating a loop to change the first letter of a sentence to a capitol.
This is what i did

text[0] = std::toupper(text[0]);

for (int x = 0; x <100; x ++){
loc[x] = text.find ('.');
text[loc[x]+2] = toupper(text[loc[x]+2]);
}
The first letter of the text gets capitalized and so does the first letter of the second sentence but thats all.

Any help would be very much appreciated !
Last edited on
What does the find() return, if it does not find anything? How should you handle that?

The find() actually does take two parameters. What is the purpose of the second parameter?

Are you sure that there is exactly one whitespace between dot and first letter?

Why the array "loc"?
th way i have it written now it changes the first letter of my text into a capitol
and the first letter of my second sentence into a capitol.

I cant get it to loop around and find the next period after it found the first thats my problem at this point
I would do it this way:


I would just take the whole sentence at a time and put it in a string called string sentence.
1
2
string sentence;
getline(fileName, sentence);

This will get the whole sentence (line) from the file and store it in string sentence.

Then, I would convert whole sentence to lower case and I wrote a little function for that.


// this function takes your sentence as parameter and return this sentence in lowercase
1
2
3
4
5
6
7
8
9
10
11
12
13
string lowerCase(string sentence)            
{    
    for (int i=0; i<sentence.length(); i++)
    {
        sentence[i] = tolower(sentence[i]);
    }
    return sentence;
}

// Then Finally, I would convert the first letter to uppercase.
// sentence[0] = toupper(sentence[0]);

cout << sentence << endl;
Last edited on
Topic archived. No new replies allowed.