FreeFoxCM

分析字符串中的字母数字符号的数量
 1/*该程序用于获取一个字符串,并从中分析数字、字母、空格以及其他字符的数量*/
 2/*编写者:FreeFox*/
 3/*2006-10-16*/
 4#include <stdio.h> 
 5int main(void)
 6{
 7 int Num=0,Wor=0,Spa=0,Oth=0,str;
 8 char c;
 9
10 printf("Place input a string:");
11 for (;(c=getchar())!='\n'; )/*获取字符串,直到输入回车为止*/
12 {
13  if (c==' ')/*分析是否为空格*/
14  {
15   Spa+=1;
16  }

17  else
18  {
19   str=(int)(c);/*把字符转化为ASCII*/
20   if ((str>=65)&&(str<=90)||(str>=97)&&(str<=122))/*分析是否为字母*/
21   {
22    Wor+=1;
23   }
 
24   else if ((str>=48)&&(str<=57))/*分析是否为数字*/
25   {
26    Num+=1;
27   }

28   else/**其他字符*/
29   {
30    Oth+=1;
31   }

32  }

33 }

34 
35 printf("\nHave numbers:%d\nHave words:%d\nHave space:%d\nHave other char:%d\n",Num,Wor,Spa,Oth);
36 return 0;
37}

38
39
40/* CopyRight @2006 FreeFox All right reserved */
41

posted on 2008-06-17 15:09  FreeFox  阅读(162)  评论(0)    收藏  举报