我正在编写一个程序,要求用户输入字符流并打印出大写字母和小写字母的数量。我正在尝试使用一个函数来完成它,但是在打印它时遇到了一些问题..对于我输入的每个字符输入,我都得到 0, 0
感谢您帮助我理解我做错了什么:
#include <stdio.h>
#include <ctype.h>
int case_letters(int ch);
int main(void)
{
int x;
printf("please enter a some characters, and ctrl + d to see result\n");
case_letters(x);
return 0;
}
int case_letters(int ch)
{
int numOfUpper = 0;
int numOfLower = 0;
while ((ch = getchar()) != EOF)
{
if ((ch = isdigit(ch)) || ch == '\n')
{
printf("please enter a valid character\n");
continue;
}
else if ((ch = isupper(ch)))
{
numOfUpper++;
}
else if ((ch = islower(ch)))
{
numOfLower++;
}
}
return printf("%d, %d", numOfUpper, numOfLower);
}
最佳答案
您所有的 if
语句都将不同的值分配给 ch
,并且不检查 ch
的值。
例如,如果你输入了一个正确的char
,这个
if ((ch = isdigit(ch)) || ch == '\n')
会将0
赋给ch
,因为isdigit(ch)
会返回0
。我猜你需要
if ( isdigit(ch) || ch == '\n')
islower
和 isupper
相同。
关于c - 打印大写/小写字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14649550/