How would you write a function to change the case of letters in a name say "bob s. smith,karen b. wright" into "Bob S. Smith,Karen B. Wright" without using any c type functions besides to upper? This would be easy if I were to write it using arrays, but I need to process the string using c++ type.
you can use an iterator to iterate the string and on meeting a whitespace character(spacebar), it copies the next letter, changes it to uppercease, constructs a new string and return it.
#include <iostream>
int main ( void )
{
std::string str("this is just a test string blah a a a a a la lal asdf.");
std::cout << str << std::endl;
if( str[ 0 ] > 96 )
{
str[ 0 ] = toUpper( str[ 0 ] );
}
for(unsignedint i = 1; i < str.size(); i++ )
{
if( str[ i ] == ' ' && str[ i+1 ] != ' ' && str[ i+1 ] > 96 )
{
str[ i+1 ] = toUpper( str[ i+1 ] );
}
}
std::cout << str << std::endl;
return( 0 );
}
this is just a test string blah a a a a a la lal asdf.
This Is Just A Test String Blah A A A A A La Lal Asdf.
Process returned 0 (0x0) execution time : 0.320 s
Press any key to continue.
This would capitalize the first letter in the string. Is there a way to set the conditions to capitalize after " " or "," ? I am totally new to programming in general and have been search for a while.
strings are c++ I am pretty sure of because in c they use arrays of characters (which a string is basically) and all I did was check each character in the string if it was a space or a comma and my code isn't very hard it's only 4 lines of code besides the brackets and the original string
and it looks to me like you want an array of strings because it seems like those should be two different names
e
like
std::string str[2] = { "jane r. white" , "jake t. great" };
seems kind of pointless to have an array of strings in one string then break them apart by searching for the comma
and I am editing my oiginal post I was thinking you couldn't use toUpper but looks like you can.
Oh now I understand! Thanks so much! So it depends on the way you declare the variable. I was wondering how someone would do this without checking each character alone. Thanks again!