i am new to c++ still but i sort of know what im doing. i understand that when cin takes a string it turns each element into ASCII then when you output it it goes back to the original char. so when i wrote this program i thought that when i took each element and manipulated it it would yield a different result. i want to take every letter that is upper case and put it to lowercase and vice versa. Im not getting something about the string class either so any type of explanation would be appreciated.
int main(void)
{
usingnamespace std;
string str;
int size;
bool digit, upper, lower;
cout << "What is the string that you would like to have manipulated? ";
getline(cin,str, '\n');
size = str.size();
cout << size << endl;
for (int i = 0; i < size; i++)
{
if (islower(str[i]) == true)
{
toupper(str[i]) ;
cout << str[i] << endl;
}
else
{
cout << tolower(str[i]);
}
}
What problems are you having with it? It looks fine to me. Line 16, the == true part is unnecessary since islower returns a bool anyway, 1 if the char is a lowercase, 0 if it's not, so you could have simply written that as (islower(str[i])).
Now that I think about it though, I believe the toupper and tolower functions return the char that was modified and don't modify the string directly. So you would have to assign the return to the string that's to be modified, ie:
str[i] = toupper(str[i]);
As it is right now it's returning the modified character but doing nothing with it, and the string is being left in its original state.
thank you for the fast response. That was a simple mistake that i was missing. Now with the modified code it still doesnt yield a result i was looking for.. It will take the upper case letters and leave them as uppercase and take the lowercase letters and display them as numbers. i would just switch the if statement conditional but i havent at the risk of missing something conceptually again.
[code
if (islower(str[i]))
{
str[i] = toupper(str[i]) ;
cout << str[i] << endl;
}
else
{
str[i] = tolower(str[i]);
cout << str[i];
}
][/code]