- #include <stdio.h>
- //设定字符串的长度
- #define length 100
- int main(int argc, const char * argv[])
- {
- //设置一个字符串数组
- char string[length];
-
- //输入一个字符串
- for (int i = 0; i <100; i ++) {
- scanf("%c",&string[i]);
- //当输入回车键时,输入结束
- if (string[i] == '\n') {
- break;
- }
- }
- //查看输入的字符串
- printf("输入的字符串是: %s\n",string);
-
- //初始化四个字母的计数器
- int countA = 0, countB = 0, countC = 0, countD = 0;
-
- //计算字母的长度
- for (int i = 0; i < length; i ++) {
-
- if (string[i] == 'A') {
- countA ++;
- }else if (string[i] == 'B')
- {
- countB ++;
- }else if (string[i] == 'C')
- {
- countC ++;
- }else if (string[i] == 'D')
- {
- countD ++;
- }
- }
-
- //打印出字母的数量
- printf("countA = %d\n",countA);
- printf("countB = %d\n",countB);
- printf("countC = %d\n",countC);
- printf("countD = %d\n",countD);
-
- //数字和字母的临时变量
- int tempCount = 0, k;
- char tempLetter;
-
- //将字母的数量和字母分别放入数组
- int countArr[] = {countA,countB,countC,countD};
- char nameArr[] = {'A','B','C','D'};
-
- //将数量进行升序排序
- for (int i = 0; i < 4; i ++) {
- for (int j = i + 1; j < 4; j ++) {
-
- //每次遍历都重置k的初始值
- k = i;
-
- //后面的数比前面的大,就把k换成大的数的下标
- if (countArr[j] > countArr[k]) {
- k = j;
- }
- //如果后面的数字比前面的大,就交换数字和字母
- if (k != i) {
- tempCount = countArr[i];
- countArr[i] = countArr[j];
- countArr[j] = tempCount;
-
- tempLetter = nameArr[i];
- nameArr[i] = nameArr[j];
- nameArr[j] = tempLetter;
- }
- }
- }
-
- //打印排序后的结果
- for (int i = 0; i < 4; i ++) {
- printf("%c = %d\n",nameArr[i],countArr[i]);
- }
-
-
- return 0;
- }
复制代码
|
|