您的位置:首页 > 运维架构 > Linux

Linux下的C语言基础编程——统计字符

2016-10-14 23:16 218 查看
输入一行文字,找出其中大写字母、小写字母、数字、空格、数字及其他字符各有多少。

这是谭浩强老师主编的书上的一道题,今天把代码给大家附上

#include <stdio.h>

int main()
{
int upper = 0;
int lower = 0;
int digit = 0;
int space = 0;
int other = 0;
int i=0;
char *p;
char s[20];
printf("input string:  ");

while ((s[i] = getchar()) != '\n') i++;
p=&s[0];

while (*p != '\n')
{
if (('A' <= *p) && (*p <= 'Z'))
++upper;
else if (('a' <= *p) && (*p <= 'z'))
++lower;
else if (*p == ' ')
++space;
else if ((* p<= '9') && (*p >= '0'))
++digit;
else
++other;
p++;
}
printf("upper case:%d     lower case:%d",upper,lower);
printf("     space:%d     digit:%d      other:%d\n",space,digit,other);

return 0;
}

下面附上运行图

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux c语言 编程