大神们新手一个有关问题T T
大神们新手求助一个问题T T
分别编写判断一个字符是否为小写字母、大写字母、数字的函数;
并编写主函数,统计用户输入的一行字符中既不是大、小写字母又
不是数字的其他字符个数
函数我编出来是这样:
#include<stdio.h>
int test(char i){
if(i>='a'&&i<='z')
return 0;
else if(i>='A'&&i<='Z')
return 0;
else if(i>='0'&&i<='9')
return 0;
else
return 1;
}
主体:
#include<stdio.h>
main()
{
int n=0;
char m;
while(getchar()!='\n')
{
m=getchar();
n+=test(m);
}
printf("既不是大小写字母也不是数字的字符有%d个\n",n);
}
请问哪里错了,为什么运行结果不对呢
------解决方案--------------------
分别编写判断一个字符是否为小写字母、大写字母、数字的函数;
并编写主函数,统计用户输入的一行字符中既不是大、小写字母又
不是数字的其他字符个数
函数我编出来是这样:
#include<stdio.h>
int test(char i){
if(i>='a'&&i<='z')
return 0;
else if(i>='A'&&i<='Z')
return 0;
else if(i>='0'&&i<='9')
return 0;
else
return 1;
}
主体:
#include<stdio.h>
main()
{
int n=0;
char m;
while(getchar()!='\n')
{
m=getchar();
n+=test(m);
}
printf("既不是大小写字母也不是数字的字符有%d个\n",n);
}
请问哪里错了,为什么运行结果不对呢
------解决方案--------------------
- C/C++ code
#include<stdio.h> int test(char i){ if(i>='a'&&i<='z'||i>='A'&&i<='Z') return 0; else if(i>='0'&&i<='9') return 0; else return 1; } main() { int n=0; char m; while((m=getchar())!='\n') { n+=test(m); } printf("既不是大小写字母也不是数字的字符有%d个\n",n); }
------解决方案--------------------
楼上给出的代码可以,其实也可以调用库函数isdigit(),isupper(),islower(),这些函数即可,这些函数在ctype.h文件中
------解决方案--------------------
原因在于循环中多了一次getchar(),改成这样即可:
- C/C++ code
while((m=getchar())!='\n') { //m=getchar(); n+=test(m); }