C++ uppercase first letter of each word in a list

hello, i am new to programming, how do i change the first letter of each word in a list of names?
does this make any sense;

1
2
3
4
5
6
7
8
9
10
11
12
13
   for(int a=0; a < phrase.length(); ++a)
         {
             phrase.at(a) = toupper(phrase.at(a));
         }
        
        if (phrase(a).SIZE(0) = toupper(phrase(a).SIZE(0)))
        {
            
        
           return phrase;
        }
}
What you did would turn all letters uppercase, one by one. I am really lost as what the if-clause would do in your code, but I suspect you should use == instead of =. My example works differently:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// define proper function somewhere 
// and decide which characters are allowed as word separators
// ' ' (space) is one of them obviously.
bool isWordSeparator( char c ) ;

// ...

for(int a=0; a < phrase.length(); ++a)
{
	if( a == 0 ) // first letter of a sentence
	{
		phrase.at(a) = toupper(phrase.at(a));
	}
	// check if preceding letter was word separator, 
	// which would imply this letter is first in a word.
	// Be careful not to check a = 0-1 here, to avoid crash
	else if( isWordSeparator(phrase.at(a-1)) ) 
	{
		phrase.at(a) = toupper(phrase.at(a));
	}
}
return phrase;
Last edited on
Topic archived. No new replies allowed.