【问题标题】:How do you make a string lowercase (the easy way)? (C++) [duplicate]你如何使字符串小写(简单的方法)? (C++)[重复]
【发布时间】:2017-11-06 02:13:30
【问题描述】:

我正在构建一个程序,该程序接受输入,然后检测到是否输入了特定内容。 输入该行后,它应该将其转换为小写。但是,我无法弄清楚如何。 我试过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;
}

我们将不胜感激!

【问题讨论】:

    标签: c++ string lowercase


    【解决方案1】:

    tolower 适用于单个字符。请注意,文档还指出非字母字符未经修改通过。

    这看起来也是使用std::transform 的好位置。

    如果您查看 std::transform 的示例,则有一个使用 std::toupper 转换字符串的示例。尝试根据您的需要调整该示例。

    【讨论】:

      猜你喜欢
      • 2010-11-17
      • 1970-01-01
      • 2014-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-16
      相关资源
      最近更新 更多