用数组求输入一行字符中的字母数字空格以及其他字符
好像对数组还是不太了解,求大佬帮助一下#include <stdio.h>
int main()
{
int a,i;
int zimu=0,shuzi=0,space=0,other=0;
gets(a);
if ((a<='z'&&a>='a')||(a<='Z'&&a>='A'))
{
zimu++;
}
else if (a==' ')
{
space++;
}
else if (a<='9'&&a>='0')
{
shuzi++;
}else
{
other++;
}
printf("%d %d %d %d\n",zimu,shuzi,space,other);
return 0;
}
#include <stdio.h>
int main()
{
char a,i;
int zimu=0,shuzi=0,space=0,other=0;
gets(a);
for(i=0;a;i++)
{
if ((a<='z'&&a>='a')||(a<='Z'&&a>='A')) zimu++;
else if (a==' ') space++;
else if (a<='9'&&a>='0') shuzi++;
else other++;
}
printf("%d %d %d %d\n",zimu,shuzi,space,other);
}
1.a应该是字符串,不是整数
2.查找字母、数字、空格、其它时应该用for循环,每次读取一个字符,进行判断,i从0到字符串长度-1
完整代码如下:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a;
int i;
int zimu=0,shuzi=0,space=0,other=0;
gets(a);
for(i=0;i<strlen(a);i++){
if ((a<='z'&&a>='a')||(a<='Z'&&a>='A')){
zimu++;
}
else if (a==' '){
space++;
}
else if (a<='9'&&a>='0'){
shuzi++;
}else {
other++;
}
}
printf("%d %d %d %d\n",zimu,shuzi,space,other);
return 0;
}
页:
[1]