用C语言编写一段程序,要求:输入一段字符,分别统计出其中的英文字母、空格、数字和其他字符的个数。

用C语言编写一段程序,要求:输入一段字符,分别统计出其中的英文字母、空格、数字和其他字符的个数。要求:输入一段字符,分别统计出其中的英文字母、空格、数字和其他字符的个数。

#include <stdio.h>
#include <conio.h>

#define LEN 50

int main (void) {
char str[LEN];
char *p = str;
char cap[LEN], low[LEN], num[LEN], space[LEN], oth[LEN]; /*分类存储字符*/
int capCnt, lowCnt, numCnt, spaceCnt, othCnt; /*分类计数*/

capCnt = lowCnt = numCnt = spaceCnt = othCnt = 0;
puts ("输入字符串:");
gets (str);

while (*p) {
if (*p>='A'&&*p<='Z') {
cap[capCnt] = *p;
capCnt++;
}
else if (*p>='a'&&*p<='z') {
low[lowCnt] = *p;
lowCnt++;
}
else if (*p>='0'&&*p<='9') {
num[numCnt] = *p;
numCnt++;
}
else if (*p==' ') {
space[spaceCnt] = *p;
spaceCnt++;
}
else {
oth[othCnt] = *p;
othCnt++;
}
p++;
}
*p = cap[capCnt] = low[lowCnt] = num[numCnt] = space[spaceCnt] = oth[othCnt] = '\0'; /*字符串结束符*/ 
putchar ('\n');

printf ("大写字母%d个:%s\n", capCnt ,cap);
printf ("小写字母%d个:%s\n", lowCnt ,low);
printf ("数字%d个:%s\n", numCnt ,num);
printf ("空格%d个:%s\n", spaceCnt ,space);
printf ("其他字符%d个:%s\n", othCnt ,oth);
putchar ('\n');

getch (); /*屏幕暂留*/ 
    return 0;
}

运行结果

温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-01-10
秦时明月汉时关,万里长征人未还。
第2个回答  2018-01-10
你大一?
第3个回答  2018-01-10
uds from another, old fellow.""No more can I