Dzwebs.Net

撰写电脑技术杂文十余年

C程序的算法:统计出字符串中英文、空格、数字和其它字符的个数

Admin | 2007-12-20 20:59:19 | 被阅次数 | 11330

温馨提示!

如果未能解决您的问题,请点击搜索;登陆可复制文章,点击登陆

  问题需求:输入一行字符后,如何分别统计出其中英文字母、空格、数字和其它字符的个数。

  程序分析:利用while语句,条件为输入的字符不为'\n'.

  程序源代码:

  #include "stdio.h"
  main()
  {

   char c;
   int letters=0,space=0,digit=0,others=0;
   printf("please input some characters\n");
   while((c=getchar())!='\n')
   {
   if(c>='a'&&c<='z'||c>='A'&&c<='Z')
    letters++;
   else if(c==' ')
    space++;
     else if(c>='0'&&c<='9')
         digit++;
       else
         others++;
  }

  printf("all in all:char=%d space=%d digit=%d others=%

  d\n",letters,space,digit,others);

  }


该杂文来自: 算法杂文

上一篇:C算法如何求最大公约数和最小公倍数

下一篇:C语言算法之不同位数的相同数字相加的分析

网站备案号:

网站备案号:滇ICP备11001339号-7

版权属性:

Copyright 2007-2021-forever Inc. all Rights Reserved.

联系方式:

Email:dzwebs@126.com QQ:83539231 访问统计