gpt4 book ai didi

c++ - C++中的字符串、字符比较

转载 作者:行者123 更新时间:2023-11-28 00:47:29 26 4
gpt4 key购买 nike

*您好!我正在制作用户输入句子和程序的程序打印出句子中有多少个字母(大写和非大写)。我做了一个程序,但它打印出奇怪的结果。请尽快提供帮助。 :)

include <iostream>
include <string>
using namespace std;

int main()
{
string Sent;

cout << "Enter a sentence !"<<endl;
cin>>Sent;

for(int a=0;a<Sent.length();a++){

if (96<int(Sent[a])<123 || 64<int(Sent[a])<91){
cout << "this is letter"<< endl;
}else{
cout << "this is not letter"<< endl;
}

}



}

最佳答案

首先,您会得到一个且只有一个词。 cin >> Sent不会提取整行。你必须使用 getline 为了做到这一点。

其次,你应该使用 isspace isalpha 而是检查字符是否为空格/字母数字符号。

第三,a < b < c本质上与 (a < b) < c 相同,这根本不是您的意思 ( a < b && b < c)。

关于c++ - C++中的字符串、字符比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15734417/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com