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

来源:百度知道 编辑:UC知道 时间:2024/07/06 23:00:37
谢谢大家!请用C语言编程

#include<stdio.h>
main( )
{
int upper=0,lower=0,digit=0,space=0,other=0,i=0;
char *p,s[20];
printf("Input string:");
while((s[i]=getchar())!=’\n’)
i++; /*从缓冲区逐个读取字符赋给字符数组 */
p=&s[0]; /* 指针指向字符串的起始字符 */
while(*p!=’\n’) /* 当串未结束时,逐个字符统计 */
{ if((*p>=’A’)&&(*p<=’Z’))
++upper;
else if((*p>=’a’)&&(*p<=’z’))
++lower;
else if(*p==’ ’ )
++space;
else if((*p>=’0’)&&(*p<=’9’))
++digit;
else
++other;
p++;
}
printf("upper case:%d lower case:%d ",upper,lower);
printf("space:%d digit:%d other:%d\n",space,digit,other);
}
运行结果:
Input string:Today is 2003.1.1↙
upper case:1 lower case:6 space:2 digit:6 other:2