Function that changes strings to all Capital letters with input?
Jan 22, 2014 at 6:17am UTC
I need to write a function that takes an input string and changes the string to all capital letters. I have what is on the main, but I don't understand how to do the function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include<iostream>
#include<string>
#include<ctype.h>
using namespace std;
string capitalize(string name)
{
//stuck in this area.
}
int main()
{
string name = "John" ;
capitalize(name);
cout << name << endl;
return 0;
}
Thanks for the help.
Jan 22, 2014 at 6:28am UTC
Jan 22, 2014 at 7:10am UTC
Basically you want to loop through the string and if it is lower case make it uppercase:
1 2 3
for ( int i = 0; name[i]; ++i )
if ( name[i] >= 'a' && name[i] <= 'z' )
name[i] -= ' ' ;
http://ideone.com/zFsIy6
You could how ever make it easier to read by using toupper as mentioned earlier ;P
Topic archived. No new replies allowed.