69,371
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <string.h>
void main()
{
int tongji(char str);
int i,word,number,space,others;
word = number = space = others = 0;
char a[9];
printf("please enter a string:");
gets(a); //将scanf("%s",a[9])改为这种形式
for(i=0;i<strlen(a);i++) //将10换位数组a的长度
{
if(tongji(a[i])==1) //括号方错地方了
word++;
else if(tongji(a[i])==2)
number++;
else if(tongji(a[i])==3)
space++;
else
others++;
}
printf("word:%d\n",word);
printf("number:%d\n",number);
printf("space:%d\n",space);
printf("others:%d\n",others);
}
int tongji(char str) //之前你将char类型,传递给了指向char的指针类型,
{
int m,i;
if((str<='z'&&str>='a')||(str<='Z'&&str>='A')) //应该包括'z','a','Z','A'
m=1;
else if(str>=0&&str<=9)
m=2;
else if(str==' ')
m=3;
else
m=4;
return(m);
}