【问题标题】:How do I loop through a string and convert to lowercase?如何遍历字符串并转换为小写?
【发布时间】:2020-01-16 03:58:25
【问题描述】:

我知道不久前还有一些其他问题,但我查看了这些问题仍然无法弄清楚。我正在尝试以字符串的形式获取用户输入,然后遍历该字符串,将所有大写字母转换为小写字母,以便我可以将其全部显示为小写字母。 我哪里错了?

int main()
{
    cout << "Enter Text: ";
    string Text;
    getline(cin, Text);
    for(int i=0; i<Text.length(); i++)
    {
        if(islower(Text[i]) == false)
        {
            tolower(Text[i]);
            i++;
        }
        Text[i] = Text[i];
    }
    cout << "Your text is: ";
    cout << Text;
    cout << "\n";
}

我对 C++ 很陌生,如果我说我有很多想法,即使我哪里出错了,那我就是在撒谎。第 11 行,for 循环表示它正在尝试比较两个不同的符号,但我不知道这意味着什么,或者这是否是我的问题的根源。 tolower() 所在的第 15 行说它“忽略使用纯属性声明的函数的返回值”,但我仍然不知道这意味着什么。 请帮忙。

【问题讨论】:

    标签: c++ string


    【解决方案1】:

    几点

    • tolower 如果存在则返回小写字符('A' 变为 'a''a' 不变、'9' 不变等)
    • Text[i] = Text[i]; 行不做任何事情,你想要Text[i] = tolower(Text[i]);
    • 无需检查每个字符是否为小写,tolower 会为你处理的

    简体

    #include <iostream>
    
    using namespace std;
    
    int main() {
        cout << "Enter Text: ";
        string Text; 
        getline(cin, Text);
        for (int i = 0; i < Text.length(); i++)
            Text[i] = tolower(Text[i]);
        cout << "Your text is: ";
        cout << Text;
        cout << "\n";
    }
    

    【讨论】:

      【解决方案2】:

      我建议使用 std 库算法函数 transform 来简化代码并使您和其他人更容易阅读代码。

      #include <iostream> //for cout and getline
      #include <algorithm> //for transform
      int main()
      {
          cout << "Enter Text: ";
          string Text;
          getline(cin, Text);
          //This will iterate over each character [Text.begin()-Text.end()] and then
          //replace it by a call to tolower with itself as a parameter
          transform(Text.begin(), Text.end(), Text.begin(), ::tolower);
          cout << "Your text is: ";
          cout << Text;
          cout << "\n";
      }
      

      编辑:

      正如 Remy 指出的那样,实现这一点的正确方法是使用代理 lambda,因为

      如果参数的值是,则 std::tolower 的行为未定义 既不能表示为 unsigned char 也不等于 EOF。

      transform(Text.begin(), Text.end(), Text.begin(), 
                [](unsigned char c){ return std::tolower(c); });
      

      【讨论】:

      • 这不是使用tolower() 作为std::transform() 的谓词的正确方式。有关 正确 方式的说明和示例,请参阅 this documentation
      • 这也应该适用于 asimes 答案。
      【解决方案3】:

      我建议查看 ascii 表以查看大写和小写字符的代码。 http://www.asciitable.com/

      bool islower(char in)
      {
         return !(char >= 'A' && char <= 'Z'); //we do this so if its a not an alphabet character we don't get false positives
      }
      
      
      char tolower(char in)
      {
         return char-'A' + 'a'; //essentially get its distance from the start of the
                                //alphabet and add this distance to the lowercase (only works on uppercase)
      }
      

      这一切都只是使用 ascii 值来获得你想要的,因为所有字符本质上都是整数。

      【讨论】:

        猜你喜欢
        • 2021-06-11
        • 2019-12-20
        • 1970-01-01
        • 1970-01-01
        • 2019-05-29
        • 2017-04-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多