统计小写字母,直到输入为字符串

标签 c

我用 C 编写了一个简短的代码,它计算小写字母(仅字母),当我输入数字或其他内容时,它会停止工作。这是代码:

char letter;
int num=0;
do
    if(islower(letter = getchar()))
       num++;
while(isalpha(letter));
printf("%d", num);
return 0;

我的问题是它无法正常工作(仅打印“1”作为结果)。 当下一个字符不是字母时必须停止。不确定该部分是否正确。

知道我做错了什么吗?谢谢。

最佳答案

这是怎么回事?

char letter;
int num=0;
while (isalpha(letter = getchar())) {
    if (islower(letter)) num++;
}
printf("%d", num);
return 0;

And it has to be stopped when the next character isn't alphabetical letter.

键盘输入可以缓冲 - 在按下 Enter 之前不进行处理

only print "1" as result

你能写一些在控制台中输入导致“1”输出的示例吗?

关于统计小写字母,直到输入为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43169858/

相关文章:

切割 QWORD 以获得 DWORD 并对其进行计算

c - 如何在 C 中将 ENUM 作为函数参数传递

c - 使用 %ld 转换说明符在 C 中打印 double 类型

C 检查字符串的最后一个字符是否与 X 相等

c - C89 中 scanf 缺失值

c - C 中的 fscanf csv。未分配值

c# - 从 C 调用 C# DLL;连接问题

c - 如何使用 "fcntl()"锁定和解锁 pid 文件

c - 在 C 中添加一个指向数组(指针)开头的指针

c - 以下 C 程序的输出是什么?