C语言编程:编写一个函数·

要求:统计由实参传来的一个字符串中字母,数字,空格和其他字符的个数,在主函数中输入字符串以及输出上述结果。

写上注释··谢谢···

#include <stdio.h> //包含输入输出头文件

int main()
{
char str[128]; //用来储存字符,最多127个字符,最后要放字符结束符'\0'
int alp=0,num=0,oth=0,spa=0;//用来计数
int i; //用来循环计数

printf("请输入字符串:");
scanf("%s",&str);

i=0; //初始化0
while(str[i]!='\0') //字符串字符不是结束符时执行循环
{
if(str[i]<='z'&&str[i]>='a')alp++;//是小写字母
else if(str[i]<='Z'&&str[i]>='A')alp++;//是大写字母
else if(str[i]<='9'&&str[i]>='0')num++;//是数字
else if(str[i]==' ')spa++;//是空格
else oth++;//以上全不是
i++;
}
printf("%d %d %d %d\n",alp,num,spa,oth);//打印

return 0; //主函数返回,main标准都是返回int,一般0表示没有错误,非零表示有错误。
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2008-11-27
#include<stdio.h> //包含输入输出头文件(getchar printf)
main()
{
char s; //用来储存字符
int alp=0,num=0,oth=0,i=0,spa=0;//用来计数
//读入
while((s=getchar())!=EOF){ //输入没有结束
if(s<='z'&&s>='a')alp++;//是小写字母
else if(s<='Z'&&s>='A')alp++;//是大写字母
else if(s<='9'&&s>='0')num++;//是数字
else if(s==' ')spa++;//是空格
else oth++;//以上全不是
}
printf("%d %d %d %d\n",alp,num,spa,oth);//打印
}

请不要抄袭
相似回答