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

#include <stdio.h>

int letter=0,digit=0,space=0,other=0;                        //定义全局变量

int Tong(char *ch);                                                            //函数声明

int main(void)

{

       char ch[20],k;

      

       gets(ch);

      

       Tong(ch);                                                             //进入Tong函数,将ch的首地址传给Tong;

      

       printf("字母=%d 数字=%d 空格=%d 其他字符%d",letter,digit,space,other);

      

       return 0;

}

int Tong(char *ch)

{

       int i;

       while(ch[i]!='\0')

       {

      

              if(ch[i]>='a'&&ch[i]<='z'||ch[i]>='A'&&ch[i]<='Z')                     //如果是字母,letter+1;

                     letter+=1;

              else if(ch[i]>='0'&&ch[i]<='9')                          //如果是数字,digit+1;                           

                            digit+=1;

              else if(ch[i]==' ')                            //如果是空格space+1;

                            space+=1;

              else                                           //如果是其他字符other+1;

                     other+=1;

              i++;      

       }

      

 }

上一篇:JVM 简析


下一篇:c语言变色圣诞树