【问题标题】:How to Convert a C++ String to Uppercase如何将 C++ 字符串转换为大写
【发布时间】:2014-06-18 13:29:57
【问题描述】:

我需要将 C++ 中的字符串转换为全大写。我一直在寻找一段时间,并找到了一种方法:

#include <iostream>
#include <algorithm> 
#include <string>  

using namespace std;

int main()
{
    string input;
    cin >> input;

    transform(input.begin(), input.end(), input.begin(), toupper);

    cout << input;
    return 0;
}

很遗憾,这不起作用,我收到了以下错误消息:

没有匹配函数调用'transform(std::basic_string::iterator, std::basic_string::iterator, std::basic_string::iterator,

我尝试了其他同样无效的方法。这是最接近工作的地方。

所以我要问的是我做错了什么。也许我的语法不好,或者我需要包含一些东西。我不确定。

我的大部分信息都在这里: http://www.cplusplus.com/forum/beginner/75634/ (最后两个帖子)

【问题讨论】:

    标签: c++ string uppercase


    【解决方案1】:

    toupper前需要加一个双冒号:

    transform(input.begin(), input.end(), input.begin(), ::toupper);
    

    解释:

    有两个不同的toupper函数:

    1. 全局命名空间中的toupper(使用::toupper访问),来自C。

    2. std 命名空间中的toupper(使用std::toupper 访问)具有多个重载,因此不能仅用名称简单地引用。您必须将其显式转换为特定的函数签名才能被引用,但获取函数指针的代码看起来很难看:static_cast&lt;int (*)(int)&gt;(&amp;std::toupper)

    由于您是using namespace std,因此在编写toupper 时,2. 隐藏了 1.,因此根据名称解析规则被选中。

    【讨论】:

    • @LokiAstari:关键是你想要全局的toupper,而不是std::,这突出了use namespace std 的问题——它大量污染了默认命名空间您不想只获得一两个符号。如果你只想导入几个符号,你应该只导入那些符号,而不是整个 std 命名空间。
    • @LokiAstari:不——如果你删除了using namespace std;,那么你可以在这段代码中只使用toupper,它会起作用(不需要::)。当然,您需要添加using std::transform;(或在转换之前粘贴std::),对于std中正在使用的其他符号也是如此。
    • 此外,您可能需要#include &lt;ctype.h&gt; 而不是#include &lt;cctype&gt; 才能在全局命名空间中获得toupper——但这与其他一切无关。 (&lt;cctype&gt; 允许将toupper 放在全局命名空间中,但不是必须的)
    • @leemes 如果我只想将字符串中的第一个字符转为大写,我该怎么办?我试过 ::toupper(input[0]) 但它没有用。
    • @HasanBasri toupper 返回修改后的字符,而不是就地更新它。这意味着,您需要分配回结果:input[0] = toupper(input[0])。 (这里,我没有放两个冒号,这实际上意味着我想要std::toupperusing namespace std)。
    【解决方案2】:

    提升字符串算法:

    #include <boost/algorithm/string.hpp>
    #include <string>
    
    std::string str = "Hello World";
    
    boost::to_upper(str);
    
    std::string newstr = boost::to_upper_copy("Hello World");
    

    Convert a String In C++ To Upper Case

    【讨论】:

      【解决方案3】:

      试试这个小程序,直接来自C++ reference

      #include <iostream>
      #include <algorithm> 
      #include <string>  
      #include <functional>
      #include <cctype>
      
      using namespace std;
      
      int main()
      {
          string s;
          cin >> s;
          std::transform(s.begin(), s.end(), s.begin(), std::ptr_fun<int, int>(std::toupper));
          cout << s;
          return 0;
      
      }
      

      Live demo

      【讨论】:

      • std::ptr_fun() 在 c++11 中被弃用,在 c++14 中被移除。
      【解决方案4】:

      你可以这样做:

      string name = "john doe"; //or just get string from user...
      for(int i = 0; i < name.size(); i++) {
          name.at(i) = toupper(name.at(i));
      }
      

      【讨论】:

        【解决方案5】:
        #include <iostream>
        
        using namespace std;
        
        //function for converting string to upper
        string stringToUpper(string oString){
           for(int i = 0; i < oString.length(); i++){
               oString[i] = toupper(oString[i]);
            }
            return oString;
        }
        
        int main()
        {
            //use the function to convert string. No additional variables needed.
            cout << stringToUpper("Hello world!") << endl;
            return 0;
        }
        

        【讨论】:

          【解决方案6】:

          使用 BitWise 运算符将大写字母转换为小写字母,反之亦然

          1.

          string s = "cAPsLock";
          for(char &c: s)
            c = c | ' ';        // similar to: c = tolower(c);
          cout << s << endl; // output: capslock
          
          string s = "cAPsLock";
          for(char &c: s)
            c = c & ~' ';       // similar to: c = toupper(c);
          cout << s << endl; // output: CAPSLOCK
          

          PS:更多信息请查看link

          【讨论】:

            【解决方案7】:

            您也可以使用下面代码中的函数将其转换为大写。

            #include<iostream>
            #include<cstring>
            
            using namespace std;
            
            //Function for Converting Lower-Case to Upper-Case
            void fnConvertUpper(char str[], char* des)
            {
                int i;
                char c[1 + 1];
                memset(des, 0, sizeof(des)); //memset the variable before using it.
                for (i = 0; i <= strlen(str); i++) {
                    memset(c, 0, sizeof(c));
                    if (str[i] >= 97 && str[i] <= 122) {
                        c[0] = str[i] - 32;    // here we are storing the converted value into 'c' variable, hence we are memseting it inside the for loop, so before storing a new value we are clearing the old value in 'c'.
                    } else {
                        c[0] = str[i];
                    }
                    strncat(des, &c[0], 1);
                }
            }
            
            int main()
            {
                char str[20];  //Source Variable
                char des[20];  //Destination Variable
            
                //memset the variables before using it so as to clear any values which it contains,it can also be a junk value.
                memset(str, 0, sizeof(str));  
                memset(des, 0, sizeof(des));
            
                cout << "Enter the String (Enter First Name) : ";
                cin >> str; //getting the value from the user and storing it into Source variable.
            
                fnConvertUpper(str, des); //Now passing the source variable(which has Lower-Case value) along with destination variable, once the function is successfully executed the destination variable will contain the value in Upper-Case
            
                cout << "\nThe String in Uppercase = " << des << "\n"; //now print the destination variable to check the Converted Value.
            }
            

            【讨论】:

            • 问题明确要求 C++ 字符串
            • 您可能需要考虑删除此提交。
            猜你喜欢
            • 2021-06-11
            • 2012-01-08
            • 1970-01-01
            • 2010-10-18
            • 2018-09-16
            • 1970-01-01
            • 2021-10-05
            • 1970-01-01
            相关资源
            最近更新 更多