converting from upper to lower case and vice-versa
I am writing code to enter a string and convert all its uppercase letters to lowercase and vice versa:
#include <iostream>
#include 开发者_运维知识库<string>
using namespace std;
int main(){
string s;
cout<<"enter the string :"<<endl;
cin>>s;
for (int i=0;i<s.length();i++){
if ('a'<=s[i] && s[i]<='z'){
s[i]=char(((int)s[i])-32);
}
if ('A'<=s[i] && s[i]<='Z'){
s[i]=char(((int)s[i])+32);
}
}
cout<<"modified string is : "<<s<<endl;
return 0;
}
Problem is that it always returns string with all lower case letters and none of them is upper case. Why?
You're converting all lower case to upper case in the first if-statement. However, the same letters that were changed to uppercase will immediately be changed to lower case again in the second if-statement.
What you want is an else if
.
Your mistake is that you convert the string to lower case after converting to upper. You can fix it like this:
if ('a'<=s[i] && s[i]<='z'){
s[i]=char(((int)s[i])-32);
}
else if ('A'<=s[i] && s[i]<='Z'){
s[i]=char(((int)s[i])+32);
}
Here is a more succinct way of doing this:
char InvertCase(char c)
{
return islower(c) ? toupper(c) : tolower(c);
}
transform(s.begin(), s.end(), back_inserter(result), InvertCase);
Check your logic. If the letter is lowercase, you convert it to uppercase. Right after that, if the letter is uppercase (which would be, if originally lowercase), you will convert it to lowercase.
Because right after you convert into upper case, you go and convert back to lower case.
By the way, why don't you use toupper
and tolower
?
this is a code i got from a blog, this might help solve your problem:
// Converting a string from lowercase to uppercase
#include <iostream>
using namespace std;
#include <cctype> // prototypes for islower and toupper
void convertToUppercase( char * );
int main()
{
char phrase[] = "characters and $32.98";
cout << "The phrase before conversion is: " << phrase;
convertToUppercase( phrase );
cout << "\nThe phrase after conversion is: "
<< phrase << endl;
return 0; // indicates successful termination
} // end main
// convert string to uppercase letters void convertToUppercase( char *sPtr ) { while ( *sPtr != '\0' ) { // current character is not '\0'
if ( islower( *sPtr ) ) // if character is lowercase,
*sPtr = toupper( *sPtr ); // convert to uppercase
++sPtr; // move sPtr to next character in string
} // end while
} // end function convertToUppercase
for the expalination of this code,visit the below link:
http://www.programmingtunes.com/converting-a-string-from-lowercase-to-uppercase/
I know that the purpose of this question was to get help debugging your own program, but I can't help sharing a neat trick which will help you accomplish what you want in a much more elegant and succinct manner:
int main()
{
string s;
cout<<"enter the string :"<<endl;
cin>>s;
for (int i=0;i<s.length();i++) s[i]^=32;
cout<<"modified string is : "<<s<<endl;
return 0;
}
This method essentially uses the fact that the difference in ASCII codes of the lowercase and uppercase counterparts of a character is 32=2^5. Hence the conversion boils down to switching the 6th lowest significant bit of the current character, which is accomplished using '^' (XOR).
精彩评论