//5、 从键盘输入一大堆字符串,统计A、B、C、D的出现次数,最后出现次数由高到低输出字母和出现次数。(C语言)
#include"stdio.h"
#include"string.h"
//定义一个结构体使字母和对应的个数绑定
struct str
{
char string;
int counts;
};
int main(void)
{
int a = 9;
int *p = &a;
int **q = &p;
char str[] = "ios my \0 lo";
char str1[100];
char temp0 = 0,temp1 = 0, temp2 = 0,temp3;//定义的临时变量
struct str stri[4];
//1、定义一个变量 用于接收键盘输入的字符
char keStr[10000];
int countAs=0,countBs=0,countCs=0,countDs=0;
int i=0;
int len=0;
stri[0].string = 'A';
stri[1].string = 'B';
stri[2].string = 'C';
stri[3].string = 'D';
//2、统计ABCD出现的次数
printf("请输入字符串\n");
gets(keStr);
while(keStr[i] != '\0')
{
len++;
i++;
}
for(i = 0 ; i<len ; i++)
if(keStr[i] != '\0')
{
switch(keStr[i])
{
case 'A':
countAs++;
break;
case 'B':
countBs++;
break;
case 'C':
countCs++;
break;
case 'D':
countDs++;
break;
}
}
printf("countAs = %d\ncountBs = %d\ncountCs = %d\ncountDs = %d\n",countAs,countBs,countCs,countDs);/**/
stri[0].counts = countAs;
stri[1].counts = countBs;
stri[2].counts = countCs;
stri[3].counts = countDs;
//比较大小、排序
for( temp0=4 ; temp0>=1 ; temp0--)
{
for( temp1=0 ; temp1<temp0-1; temp1++ )
if(stri[temp1].counts<stri[temp1+1].counts)
{
temp2 = stri[temp1].counts;
temp3 = stri[temp1].string;
stri[temp1].counts = stri[temp1+1].counts;
stri[temp1].string = stri[temp1+1].string;
stri[temp1+1].counts = temp2;
stri[temp1+1].string = temp3;
}
}
//打印最终结果
for( temp0=0 ; temp0<4 ; temp0++)
{
printf("%c的个数是: ",stri[temp0].string);
printf("%d\n",stri[temp0].counts);
}
printf("请输入字符串\n");
gets(str1);
str;
puts(str1);
puts(str);
printf("*p = %d\n",*p);
printf("**q = %d\n",**q);
}
|
|