文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>C程序习题-统计字符类型个数[8.9]

C程序习题-统计字符类型个数[8.9]

时间:2010-08-10  来源:chengxiaopeng

    编写一函数,由实参传来一个字符串,统计此字符串中的字母,数字,空格和其他字符的个数,在主函数中输入字符串以及输出上述结果。     我们可以单独写一个函数,顺序读取每一个字符,判断字符的类型,进行自加一操作。然后主函数再输出字符类型的个数。  

#include <stdio.h>

int i1 = 0,i2 = 0,i3 = 0,i4 = 0;

void tongji(char[]);
int main(int argc, char *argv[])
{
    char ch1[100];
    printf("please input a string:\n");
    gets(ch1);
    tongji(ch1);
    printf("the char count: %d ,the number count: %d , the space count: %d ,other count :%d \n",
                i1,i2,i3,i4);
    
    system("pause");
    return 0;
}

void tongji(char ch[])
{
     int i;
     char c;
     for (i = 0; (c = ch[i]) != '\0'; i++)
     {
         if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
         {
            i1 ++;
         }
         else if (c >= '0' && c <= '9')
         {
              i2 ++;
         }
         else if (c == ' ')
         {
              i3 ++;
         }
         else
         {
             i4 ++;
         }
     }
}


相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载