代码之家  ›  专栏  ›  技术社区  ›  Anonymous

如何使字符串小写(简单的方法)?(C++)[副本]

  •  0
  • Anonymous  · 技术社区  · 7 年前

    我正在构建一个程序,它接受输入,然后检测并查看输入的特定内容。 线路输入后 应该 将其转换为小写。但是,我不知道怎么做。 我已经试过tolower(),但我不知道该把什么放在括号里。代码如下:

    #include <string>
    #include <sstream>
    #include <iostream>
    #include "stdafx.h"
    using namespace std;
    int main()
    {
        while (1)
        {
            string dir = "C:/";
            string line;
            string afterPrint;
            string prompt = "| " + dir + " |> ";
            cout << prompt;
            cin >> line;
    
            stringstream ss(line);
            while (ss)
            {
                string command;
                ss >> command;
                command = ;// Command, but lowercase
                if (command == "cd")
                {
                    string dir;
                    if (ss >> dir)
                    {
                        cout << "changedir: " << dir << "\n";
                        string prompt = "| " + dir + " |> ";
                    }
                }
                else if (command == "c:" || command == "c:\\" || command == "c:/")
                {
                    cout << "Directory: " << dir << "\n";
                }
                else
                {
                    cout << "error\n";
                }
                break;
            }
        }
        return 0;
    }
    

    非常感谢您的帮助!

    1 回复  |  直到 7 年前
        1
  •  1
  •   Paul Belanger    7 年前

    tolower 处理单个字符。请注意,文档中还指出,非alpha字符未经修改就可以传递。

    这看起来也是一个很好的使用位置 std::transform .

    如果您在示例中查找 标准::转换 ,有一个使用std::toupper转换字符串的示例。试着根据你的需要修改这个例子。