如何使字符串小写(最简单的方法)? (C++)
How do you make a string lowercase (the easy way)? (C++)
我正在构建一个接受输入的程序,然后检测是否输入了特定内容。
输入该行后,应该将其转换为小写。但是,我无法弄清楚如何。
我试过 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 转换字符串的示例。尝试根据您的需要调整该示例。
我正在构建一个接受输入的程序,然后检测是否输入了特定内容。 输入该行后,应该将其转换为小写。但是,我无法弄清楚如何。 我试过 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 转换字符串的示例。尝试根据您的需要调整该示例。