0

How do you use for loop to capitalize each letter in a word?

When I type in the name, the first letter of the second word is capitalized but different. For example, I get Homer Oimpson when I type in "homer simpson". I have this part in my code: string fullName; cout << "Enter full name: \n"; getline(cin, fullName) fullName[0] = toupper(fullName[0]); for (int i =1; i<size; i++ { if (full name[i-1] == ' ') fullName[i] = toupper(fullName[i]); } cout << fullName << endl;

27th Feb 2018, 1:57 AM
Chuck
Chuck  - avatar
1 Odpowiedź
+ 5
That's quite true, me also consider a space as word separator and so used it as a point where capitalization is necessary. I would however suggest you to check each character to ensure a correct output, just in case the input was "baRt siMpson" a simple capitalization after a space will not fix the "R" in "baRt" nor the "M" in "siMpson". Here's my code related to character case conversion: https://code.sololearn.com/cM3qp41znqpZ/?ref=app
27th Feb 2018, 2:20 AM
Ipang