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

标签 c++ string char ascii string-comparison

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

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/

相关文章:

c++ - 动态数组和字符串出现问题 (C++)

r - 如何使用来自 R 中其他列的值格式化字符串

c++ - 迭代 char* 错误没有从 char* 到 int 的转换

c++ - 在编译时生成唯一编号

c++ - R 和 Simmer : Performance boost on large data frames

c++ - Haar 检测 - 保存图像的 Mat 以获取和显示前一帧

string - 用 Lua 匹配整个、精确的文本行

ios - 如何让程序填写文本字段,并确保用户无法编辑程序填写的内容?

c# - 将字符串、char类型转换为System.Windows.Forms.Keys

C++将字符转换/添加到字符串