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