编写一个C语言小程序

输入一行字符,编写的程序能分别统计出其中英文字母、空格、数字和其它字符个数

程序一
#include<stdio.h>
void main()
{
char c;
int en = 0, space = 0, num = 0, other = 0;

do
{
scanf("%c", &c);
if (c >= '0' && c <= '9')
num++;
else if (c == ' ')
space++;
else if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')
en++;
else
other++;
}while (c != '\n');
other--;

printf("\n English words: %d\n Space: %d\n Num: %d\n Other: %d\n", en, space, num, other);
getch();
}

程序二:
# include "stdio.h"
# include "conio.h"
void main( )
{
int upper=0,lower=0,digit=0,space=0,other=0,i=0;
char *p,s[80];
printf("\nInput a string:");
while ((s[i]=getchar())!='\n') i++;
p=s;
while(*p!='\n')
{if((*p>='A')&&(*p<='Z'))
upper++;
else if((*p>='a')&&(*p<='z'))
lower++;
else if(*p==' '||*p==9)
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 ",space,digit,other);
getch();
}
温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网