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

来源:互联网 发布:nasa数据查询 编辑:程序博客网 时间:2024/06/02 17:00

输入一行文字,找出其中大写字母、小写字母、数字、空格、数字及其他字符各有多少。

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

#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;}

下面附上运行图


0 0