编写一函数 void count(char str[ ]),由实参传来一个字符串,统计此字符串中字母、数字、空格和其它字符的个数,在主函数中输入一行字符串(可能有空格)并调用count函数进行统计,在主函数中输出统计结果。要求函数首部必须为 void count(char str[ ])注意输入的一行字符串,注意输出的文本和格式。注意指定的调用函数的首部。这是我写的有问题#include<stdio.h>int i=0,j=0,k=0,m=0;void count(char str[ ]);int main(){ char str1[10]; scanf("%s",str1); count(str1); printf("字母有:%d个\n数字有:%d个\n空格有:%d个\n其他字符有:%d个\n",i,k,j,m);}void count(char str[]){ int b=0; gets (str); for(;str[i]!='\0';i++) {if((str[i]<='z'&&str[i]>='a')||(str[i]<'Z'&&str[i]>'A')) i++; else if(str[i]==' ') j++; else if(str[i]<58&&str[i]>47) k++; else m++; }}正确应该调试如下,哪里有问题
你的i值重复了,在count函数中i代码操作到的下标,也代表字母的个数。
应改为
#include<stdio.h>