#include <iostream>
void sentenceCapitalizer(char *ptr);
int main(){
char userString[] =
" nO, not 123 tonight. it's a very popular place \
and you have to make reservations in advance. besides, \
it's expensive, and I don't have any money.";
std::cout << "The text BEFORE the modification:\n" << userString << '\n';
sentenceCapitalizer(userString);
std::cout << "\nThe text AFTER the modification:\n" << userString << '\n';
return 0;
}
void sentenceCapitalizer(char *ptr)
{
while(*ptr != '\0')
{
*ptr = toupper(*ptr);
ptr++;
}
}
1) declare a bool variable (let’s say ‘beginning’).
--> when ‘beginning’ is true, the next character you find must be capitalized;
2) initialize ‘beginning’ to true
3) loop through your C-style char array
(note: consider switch)
if you find a character like [period, exclamation mark…]
set ‘beginning’ to truecontinueif you find a [space, comma… --> any character that can’t be capitalized]
continueif you find a char that can be capitalized
if ‘beginning’ == true
capitalize that char
set ‘beginning’ to falsecontinue
(Note: I haven’t written the code - it’s just an hint)
I suggest you follow through with your if statement idea and take careful note of the very strong hint from @Enoizat.
One small extra tip: AS a start, just go through the original string (array of characters with '\0' at its end) character by character in a new loop and change every 'a' to a '*' just to prove you can control what's going on. Then build up to do the real job with more complex if's with and's (&&) and or's (||) :)
(BTW you don't have to delete your MAX_LENGTH stuff necessarily, both work, my way is not restricted by any particular string length)