Monday, May 24, 2010

Help with a c++ program that converts uppercase to lowercase and vice versa?

I've been working on this program and I can get it to work, but it only works with a single character. I want to be able to enter a few letters in and have them all converted to the opposite case. I'm new to arrays so if someone could please help me I'd appreciate it. This is what I have so far:





#include %26lt;cctype%26gt;


#include %26lt;iostream%26gt;





using namespace std;





int main()


{


char ch;





cout%26lt;%26lt;"Enter a few letters: ";


cin%26gt;%26gt; ch;





if ( isalpha ( ch) ) {


if ( isupper ( ch) ) {


ch = tolower ( ch );





cout%26lt;%26lt;"The lower case equivalent is "%26lt;%26lt; ch %26lt;%26lt;endl;


}


else {


ch = toupper ( ch);





cout%26lt;%26lt;"The upper case equivalent is "%26lt;%26lt; ch %26lt;%26lt;endl;


}


}


else


cout%26lt;%26lt;"The character is not a letter"%26lt;%26lt;endl;





cin.get();


return 0;


}

Help with a c++ program that converts uppercase to lowercase and vice versa?
int i;





while (string[i]) {


string[i++] = toupper( string[i] );


}
Reply:You have no arrays in this at all. ch is declared as a char, not as an array of char, which is what you would want. try doing a global replace of chi[i] for ch, and then replacing it i with -- say 20 in the declaration section. Then put everything in a for loop or a while loop (while (ch[i]!='\0'"){ and make sure you initialize i to 0 and do i++ in your program.)


No comments:

Post a Comment