问题描述:
有一篇文章,共有3行文字,每行有80个字符。要求分别统计出其中英文大写字母、小写字母、数字、空格以及其他字符的个数。
gets(a[i]);一定要标清楚是字符串数组的第几个位置,包括空格字符。回车结束输入。
代码实现:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,j,WORD,word,dig,space,oth;//i行,j列,大写字母WORD,小写字母word,空格,其他
char text[3][80];
WORD=word=dig=space=oth=0;
for(i=0;i<3;i++)
{
printf("请输入第%d行文本:",i+1);
gets(text[i]);
for(j=0;j<80&&text[i][j]!='\0';j++)
{
if(text[i][j]>='A'&&text[i][j]<='Z')WORD++;
else if(text[i][j]>='a'&&text[i][j]<='z')word++;
else if(text[i][j]>='0'&&text[i][j]<='9')dig++;
else if(text[i][j]==' ')space++;
else oth++;
}
}
printf("大写字母数为:%d\n",WORD);
printf("小写字母数为:%d\n",word);
printf("数字数为:%d\n",dig);
printf("空格数为:%d\n",space);
printf("其他字符数为:%d\n",oth);
return 0;
}