本帖最后由 dawn.dai 于 2014-4-26 21:28 编辑
#include "stdio.h"
void letterCount(char *string);
int main(int argc, const char * argv[])
{
char s[200] ;
printf("输入字符串:");
scanf("%s",s);
letterCount(s);
return 0;
}
void letterCount(char *string ){
int A=0;
int B=0;
int C=0;
int D=0;
int *p;
int *r;
for (int i=0; i<strlen(string); i++) {
char c =string;
if (c=='A'){
A++; //3
}else if(c=='B'){
B++; //1
}else if(c=='C'){
C++; //2
}else if(c=='D'){
D++; //4
}
}
int arry[4][2]={{'A',A},{'B',B},{'C',C},{'D',D}};
for (int j=0; j<4; j++) {
for (int k=j+1; k<4; k++) {
if (arry[j][1]<arry[k][1]) {
int t =arry[j][1];
arry[j][1]=arry[k][1];
arry[k][1]=t;
int m =arry[j][0];
arry[j][0]=arry[k][0];
arry[k][0]=m;
}
}
}
printf("ABCD出现次数由高到低分别是:\n");
for (int n=0; n<4; n++) { p =&arry[n][0]; r =&arry[n][1]; printf("%c %d\n",*p,*r);
}
} |