分析一些题目可知,我们可以用两个for循环嵌套起来遍历每行的文字,然后用if语句对每一个文字进行判断,然后给相应的文字类型++,最后输出。
#include "stdafx.h"
#include"stdlib.h"
int judge(int cap, int low, int digit,int blank, int other);
char easy[3][80];
int main()
{ int cap,low,digit,blank,other;
cap = low = digit = blank = other = 0;
judge(cap, low, digit, blank, other);
system("pause");
return 0;}
int judge(int cap, int low,int digit, int blank, int other)
{ int i, j;
cap = low =digit= blank = other = 0;
for (i = 0; i < 3; i++)
{ printf("input text of this trip\n");
gets_s(easy[i]);
for (j = 0; j < 80 && easy[i][j] != '\0'; j++)
{if (easy[i][j] >= 'A'&&easy[i][j] <= 'Z')//判断是否为大写字母
cap++;
else if (easy[i][j] >= 'a'&&easy[i][j] <= 'z')
low++;
else if (easy[i][j] >= '0'&&easy[i][j] <= '9')
digit++;
else if (easy[i][j] == ' ')
blank++;
else other++;}}
printf("大写字母有%d个\n", cap);
printf("小写字母有%d个\n", low);
printf("数字有%d个\n", digit);
printf("空格有%d个\n", blank);
printf("其他字符有%d个\n", other);
return 0;
}