黑马程序员技术交流社区
标题:
从键盘输入一大堆字符串,统计A、B、C、D的出现次数,最...
[打印本页]
作者:
qiu1993
时间:
2016-2-20 22:31
标题:
从键盘输入一大堆字符串,统计A、B、C、D的出现次数,最...
#include <stdio.h>
void maopao(int a[],int len){
int temp = 0;
for (int i=0; i<len-1; i++) {
for (int j=0; j<len-1-i; j++) {
if (a[j]<a[j+1]) {
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
}
int main(int argc, const char * argv[]) {
char s[100];
printf("请输入字符串:");
scanf("%s",s);
int i=0;//延长生命周期
int countA = 0;
int countB = 0;
int countC = 0;
int countD = 0;
while (s[i]!='\0') {
if (s[i]=='A') {
countA++;
}else if (s[i]=='B'){
countB++;
}else if (s[i]=='C'){
countC++;
}else if(s[i]=='D'){
countD++;
}
i++;
}
int a[4] = {countA,countB,countC,countD};
maopao(a, 4);
for (int i=0; i<4; i++) {
if (a[i]==countA) {
printf("A出现的次数是%d\n",a[i]);
}else if (a[i]==countB){
printf("B出现的次数是%d\n",a[i]);
}else if (a[i]==countC){
printf("C出现的次数是%d\n",a[i]);
}else if(a[i]==countD){
printf("D出现的次数是%d\n",a[i]);
}
}
return 0;
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2