本帖最后由 supergcs 于 2014-11-12 15:29 编辑
- //找出输出的字符串中得A B C D出现的次数,并且按出现次数的从大到小一次输入该字母和出现次数
- #include <stdio.h>
- void compare4(int s[]);
- void charNumprintf(int k[],int x[]);
- int main() {
- int a,b,c,d;
-
- int charNumCount[4]={a,b,c,d};
-
- int charNumcopy[4]={a,b,c,d};
-
- printf("请输入一个字符串:\n");
-
- char *str;
- scanf("%s",str);
-
- char *p =str;
-
- while (*p !='\0') {
- switch (*p) {
- case 'A':
- charNumCount[0]++;
- break;
- case 'B':
- charNumCount[1]++;
- break;
- case 'C':
- charNumCount[2]++;
- break;
- case 'D':
- charNumCount[3]++;
- break;
- }
- p++;
- }
- for (int i = 0; i < 4; i++) {
- charNumcopy[i] = charNumCount[i];
- }
-
- compare4(charNumCount);
-
- charNumprintf(charNumcopy, charNumCount);
-
- return 0;
- }
- //比较并且交换数值的函数;
- void compare4(int s[])
- {
- int temp;
- for (int i=0; i<4; i++) {
- for(int j=1;j<4;j++)
- {
- if (s[j]>s[i])
- {
- temp = s[i];
- s[i] = s[j];
- s[j] = temp;
- }
- }
- }
- }
- void charNumprintf(int k[],int x[])
- {
- // char dx[5] = "abcd";
-
- for (int i =0; i < 4;i++) {
- for (int j =0; j < 4; j++) {
- if(x[i] == k[j])
- {
- printf("字母%c出现的次数是%d\n",'A'+j ,x[i]);
- }
- }
- }
- }
复制代码 |