黑马程序员技术交流社区

标题: 从键盘输入一大堆字符串,统计A、B、C、D的出现次数,最... [打印本页]

作者: qiu1993    时间: 2016-2-20 22:31
标题: 从键盘输入一大堆字符串,统计A、B、C、D的出现次数,最...
  1. #include <stdio.h>

  2. void maopao(int a[],int len){
  3.     int temp = 0;
  4.     for (int i=0; i<len-1; i++) {
  5.         for (int j=0; j<len-1-i; j++) {
  6.             if (a[j]<a[j+1]) {
  7.                 temp = a[j];
  8.                 a[j] = a[j+1];
  9.                 a[j+1] = temp;
  10.             }
  11.         }
  12.     }
  13. }

  14. int main(int argc, const char * argv[]) {
  15.     char s[100];
  16.     printf("请输入字符串:");
  17.     scanf("%s",s);
  18.    
  19.     int i=0;//延长生命周期
  20.    
  21.    
  22.     int countA = 0;
  23.     int countB = 0;
  24.     int countC = 0;
  25.     int countD = 0;
  26.     while (s[i]!='\0') {
  27.         if (s[i]=='A') {
  28.             countA++;
  29.         }else if (s[i]=='B'){
  30.             countB++;
  31.         }else if (s[i]=='C'){
  32.             countC++;
  33.         }else if(s[i]=='D'){
  34.             countD++;
  35.         }
  36.         i++;
  37.     }
  38.     int a[4] = {countA,countB,countC,countD};
  39.     maopao(a, 4);
  40.     for (int i=0; i<4; i++) {
  41.         if (a[i]==countA) {
  42.             printf("A出现的次数是%d\n",a[i]);
  43.         }else if (a[i]==countB){
  44.             printf("B出现的次数是%d\n",a[i]);
  45.         }else if (a[i]==countC){
  46.             printf("C出现的次数是%d\n",a[i]);
  47.         }else if(a[i]==countD){
  48.             printf("D出现的次数是%d\n",a[i]);
  49.         }

  50.     }
  51.     return 0;
  52. }

复制代码







欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2