输入一行字符,统计其中的英文字符、数字字符、空格和其他字符的个数。
输入提示信息:“Please input a string:”
输入字符串用gets()
输出提示信息和格式:
“English character: %d\n”
“digit character: %d\n”
“space: %d\n”
“other character: %d\n”
程序运行示例:
Please input a string:dytxx246$6@,.
English character: 5
digit character: 4
space: 0
other character: 4
=================================================
#include<stdio.h>
#include<ctype.h>
#define N 100
int main()
{
char zx[N];
int i;
int yw=0,sz=0,kg=0,qt=0;
printf("Please input a string:");
gets(zx);
for(i=0;zx[i]!='\0';i++)
{
//if(zx[i]>='a'&&zx[i]<='z' || zx[i]>='A'&&zx[i]<='Z')
if(isalpha(zx[i]))
yw++;
//else if(zx[i]>='0'&&zx[i]<='9')
else if(isdigit(zx[i]))
sz++;
//else if(zx[i]==' ')
else if(isspace(zx[i]))
kg++;
else qt++;
}
printf("English character: %d\n",yw);
printf("digit character: %d\n",sz);
printf("space: %d\n",kg);
printf("other character: %d\n",qt);
return 0;
}