9.2
输入任意长度的字符串,分别统计其中大写字母,小写字母,数字字符及其它字符的个数并输出统计结果,再按统计值从多到少依次输出。
提示:测试用例输出的冒号是中文冒号。
样例输入:
ASDFghjkl*(),;19
样例输出:
大写字母:4
小写字母:5
数字字符:2
其它字符:5
从多到少依次输出如下:
小写字母:5
其它字符:5
大写字母:4
数字字符:2
#include<stdio.h>
#include<string.h>
int main()
{
char m[100];
gets(m);
int a = 0, b = 0, c = 0, d = 0;
int i;
for (i = 0; m[i] != 0; i++)
{
if (m[i] >= 'A'&&m[i] <= 'Z')
a++;
else if (m[i] >= 'a'&&m[i] <= 'z')
b++;
else if (m[i] >= '0'&&m[i] <= '9')
c++;
else
d++;
}
char n1[4][100] = { { "大写字母:" }, { "小写字母:" }, { "数字字符:" }, { "其它字符:" } };
n1[0][10] = '0' + a;
n1[1][10] = '0' + b;
n1[2][10] = '0' + c;
n1[3][10] = '0' + d;
n1[0][11] = 0;
n1[1][11] = 0;
n1[2][11] = 0;
n1[3][11] = 0;
int j;
for (j = 0; j <= 3; j++)
printf("%s\n", n1[j]);
printf("从多到少依次输出如下:\n");
int t[4] = {0,1,2,3};
int p, q, tmp;
for (p = 0; p <3; p++)
{
for (q = 0; q < 3; q++)
{
if (n1[t[q]][10] < n1[t[q + 1]][10])
{
tmp = t[q];
t[q] = t[q + 1];
t[q + 1] = tmp;
}
}
}
int x;
for (x = 0; x <= 3; x++)
printf("%s\n", n1[t[x]]);
return 0;
}