求助
#include <stdio.h>#include <stdlib.h>
int main()
{
int chang, i = 0, j, num = 1, k;
//chang表示用户输入字符串的长度,i和j用来循环计数的,num用来表示character数组的行长度,k用来排序时作为中间值
char characters, character = {0}, K;
//characters数组用来存放用户输入的字符串,character这个二维数组用来存放characters中非重复部分元素及其出现次数,K用来排序时作为中间值
printf("请用户输入字符串:");
gets(characters);
chang = strlen(characters);
for(;i < chang;i++)
{
if(i == 0)
{
character = characters;
character = 1;
}
else
{
for(j = 0;j < num;j++)
{
if(characters == character)
{
character = character + 1;
break;
}
else if(j == num - 1)
{
character = characters;
num = num + 1;
printf("num = %d\n", num);
}
}
}
}
for(i = 0; i < chang - 1; i++)
{
for(j = i + 1;j < chang; j++)
{
if(character > character)
{
k = character;
character = character;
character = k;
K = character;
character = character;
character = K;
}
}
}
for(i = 0; i < chang; i++)
{
printf("字符%c出现的次数为%d\n", character, character);
}
printf("字符%c出现的次数最多,为%d次\n", character, character);
return 0;
}
输入aaabbc后输出如图
为什么二维数组character中还存了三个空元素呢? 本帖最后由 傻眼貓咪 于 2022-1-15 08:04 编辑
#include <stdio.h>
int main()
{
char string, *p = string;
short chr = {0}, *q = chr, m = -1, n = -1;
scanf("%s", string);
while(*p) chr[*p++]++;
for(int i = 0; i < 256; i++){
if(*(q+i)){
printf("字符 %c 出现的次数为 %hd 次\n", i, *(q+i));
if(*(q+i) > n){
m = i;
n = *(q+i);
}
}
}
printf("字符 %c 出现的次数最多,为 %hd 次", m, n);
return 0;
}aaabbc
字符 a 出现的次数为 3 次
字符 b 出现的次数为 2 次
字符 c 出现的次数为 1 次
字符 a 出现的次数最多,为 3 次
页:
[1]