输入一行字符,编写的程序能分别统计出其中英文字母、空格、数字和其它字符个数
程序一
#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();
}
#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();
}
温馨提示:答案为网友推荐,仅供参考