C++ 从大写转换为小写,反之亦然

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7426049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:55:52  来源:igfitidea点击:

converting from upper to lower case and vice-versa

c++string

提问by dato datuashvili

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?

问题是它总是返回带有所有小写字母的字符串,并且没有一个是大写字母。为什么?

回答by Mysticial

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.

您正在第一个 if 语句中将所有小写字母转换为大写字母。但是,在第二个 if 语句中,被更改为大写的相同字母将立即再次更改为小写。

What you want is an else if.

你想要的是一个else if.

回答by Don Reba

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);

回答by K-ballo

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.

检查你的逻辑。如果字母是小写,则将其转换为大写。紧接着,如果字母是大写的(如果原来是小写的话),你将把它转换成小写。

回答by littleadv

Because right after you convert into upper case, you go and convert back to lower case.

因为在您转换为大写之后,您会立即转换回小写。

By the way, why don't you use toupperand tolower?

顺便说一句,你为什么不使用toupperand tolower

回答by user2808359

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 .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'

// 将字符串转换为大写字母 void convertToUppercase( char *sPtr ) { while ( *sPtr != '\0' ) { // 当前字符不是 '\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/

http://www.programmingtunes.com/converting-a-string-from-lowercase-to-uppercase/

回答by Shubham

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).

这种方法本质上利用了一个事实,即一个字符的小写和大写对应的 ASCII 代码的差异是 32=2^5。因此,转换归结为切换当前字符的第 6 个最低有效位,这是使用 '^' (XOR) 完成的。