c++ - 如何将大写字母读成小写字母?
问题描述
聊天机器人将读取他们的输入并根据输入进行回复,但小写和大写的读取方式不同。我一直未能找到如何让这段代码将大写字母读为小写。我如何让它将输入识别为小写?
#include <iostream>
#include <cstring>
#include <locale>
#include <limits>
#include <cstdlib>
using namespace std;
int main
(int argc, char **argv)
{
string input;
cout << "Hello there" << endl;
{
for(;;)
{
std::cin >> input;
if (input == "hi")
cout << "hey what's up?" << endl;
if (input == "hey")
cout << "hey, what's up?" << endl;
if (input == "hello")
cout << "Hey, what's up?" << endl;
if (input == "how are you?")
cout << "I am good, how are you?" << endl;
if (input == "fine")
cout << "that's good" << endl;
if (input == "hru")
cout << "i am good, how are you?" << endl;
if (input == "good")
cout << "thats good" << endl;
}
}
}
解决方案
通过在进行比较之前将所有内容转换为小写。要轻松做到这一点,请#include <cctype>
使用 tolower() 函数。
推荐阅读
- php - 从表中获取值并将该值提交到另一个表mysql php
- android - Xamarin:更新 NuGet 包后无法构建:Xamarin.Build.DownloadsAaRProgGuard 失败
- python - 用python循环往后退一步
- webrtc - 有没有办法在统一计划中向收发器添加自定义属性
- postgresql - Postgres:将 ON CONFLICT 与 CASE 一起使用
- ios - 如何调整 UITextView 的大小,当达到某个值时,它会根据内容和固定宽度自动调整其宽度和高度?
- php - 带回显输出的 Var_dump
- c# - 循环遍历对象初始化器
- r - 箱线图中的日期格式(ggplot2)
- php - Using Foreach to print the values