第十五周 OJ总结<9>--统计字符串种类

Copyright (c) 2016,
烟台大学计算机与控制学院 All ringts reserved.
文件名称:OJ总结<9>--统计字符串种类
作 者:王兴振
完成日期:2016年12月11日
版 本 号:未知
题目描述:
  用指针编写一个程序,输入字符串后,统计其中各种字符的个数,输出其中大小写字母,数字,以及其他字符的个数。

主函数已经给出,请编写统计字符种类函数。

输     入:

  一串字符串

输      出:
  该字符串中大小写字母,数字,以及其他字符的个数,最后输出总字符串长度。

样例输入:
  I play LOL for 3 years.

样例输出:

4

12

1

6

23

答       案:

#include <stdio.h> 
  
  
int main() 
{ 
   char str[100]; 
   gets(str); 
   char *ptr=str; 
   void fuction(char *); 
   fuction(ptr); 
  return 0; 
}void fuction(char *a) 
{ 
     int i=0,d=0,x=0,s=0,q=0,len=0; 
     while(a[i]!='\0') 
     { 
         if(a[i]>='A' && a[i]<='Z') 
            d++; 
         else if(a[i]>='a' && a[i]<='z') 
            x++; 
         else if(a[i]>='0' && a[i]<='9') 
            s++; 
         else
            q++; 
         len++; 
         i++; 
     } 
     printf("%d\n", d); 
     printf("%d\n", x); 
     printf("%d\n", s); 
     printf("%d\n", q); 
     printf("%d\n", len); 
} 

总     结:

  和以前做的一道题类似,但用的不是一种方法。


猜你喜欢

转载自blog.csdn.net/wxz1814/article/details/53575137