c++ - 如何使字符串小写(最简单的方法)? (C++)

标签 c++ string lowercase

<分区>

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

非常感谢您的帮助!

最佳答案

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

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

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

关于c++ - 如何使字符串小写(最简单的方法)? (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47128789/

相关文章:

c++ - 在限制之间搜索 std::string

bash - 在 bash 中检查 .pgn 不区分大小写扩展名的更短方法

c# - C#中如何将字符串的一部分转为小写

c++ - 由于段错误,我的程序在 fflush 上崩溃,...但并非总是如此?

c++ - 为什么这个构造函数没有给出不完整的类型错误?

python 类型错误

Java传入字符串并返回字符串

C++删除二叉树中的最小元素

c++ - friend 类指向不同命名空间中的同名类

linux - 命令 tr 上下提取