所以我想编写一个代码,您可以在其中找到字符串中大写和小写字母的数量(没有空格) 所以我想要这样的东西:
input:
HEllO
Output:
2 3
所以我的代码是这样的:
#include<stdio.h>
int main() {
int upper = 0, lower = 0;
char ch[80];
int i;
printf("\nEnter The String : ");
gets(ch);
i = 0;
while (ch[i] != '') {
if (ch[i] >= 'A' && ch[i] <= 'Z')
upper++;
if (ch[i] >= 'a' && ch[i] <= 'z')
lower++;
i++;
}
printf("%d %d", upper, lower);
return (0);
}
代码有问题,但我找不到错误。有人可以修复它吗?谢谢。
最佳答案
更正代码-
#include <stdio.h>
int main(void)
{
int upper = 0, lower = 0;
char ch[80];
int i = 0;
printf("\nEnter The String : ");
fgets(ch, sizeof(ch), stdin);
while (ch[i] != '\0')
{
if (ch[i] >= 'A' && ch[i] <= 'Z')
upper++;
if (ch[i] >= 'a' && ch[i] <= 'z')
lower++;
i++;
}
printf("\nuppercase letter(s): %d \nlowercase letter(s): %d", upper, lower);
return 0;
}
注意:我使用了 fgets()
而不是 gets()
,因为后者存在缓冲区溢出问题。
关于c - C语言计算字符串中大小写字母的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33380592/