【发布时间】:2020-11-23 17:38:15
【问题描述】:
这个程序的目标是要求用户输入并从用户的字符串中打印出 3 个最常见的 字符。几天后,我设法完成了这种工作。我的意思是因为如果输入为“aaaaaaabbbbbbccxz”程序会正常工作,但如果输入为“abc”程序会打印错误的值。与“aabbc”、空字符串等相同。我一直在尝试解决这个问题,但没有运气。我不知道该怎么做。 这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#define N 100
/*
ask user to type letter string and load it to the array.
Count apperance of ASCII characters in the string
Print 3 most frequent characters from the string and how often they appeared.
*/
int main(int argc, char *argv[])
{
int ascii[256] = {0};
char str[N];
int z, i, j, k, top, top2, top3, index, index2, index3;
printf("Type your string: \n");
scanf("%s", &str);
for(i = 0; str[i] != 0; i++)
{
++ascii[str[i]];
}
top = ascii[0];
index = 0;
for(z = 0; str[z] != 0; z++)
{
if( ascii[str[z]] > top)
{
top = ascii[str[z]];
index = z;
}
}
printf("The most frequent is %c - was %d times.\n", str[index], top);
top2 = ascii[0];
index2 = 0;
for(j = 0; str[j] != 0; j++)
{
if( ascii[str[j]] > top2 && ascii[str[j]] < top)
{
top2 = ascii[str[j]];
index2 = j;
}
}
printf("second most frequent %c %d times.\n", str[index2], top2);
top3 = ascii[0];
index3 = 0;
for(k = 0; str[k] != 0; k++)
{
if( ascii[str[k]] > top3 && ascii[str[k]] < top2 && ascii[str[k]] < top)
{
top3 = ascii[str[k]];
index3 = k;
}
}
printf("3rd most frequent %c %d times.\n", str[index3], top3);
return 0;
}
编辑:
它有效。
#include <stdio.h>
#include <stdlib.h>
#define N 100
int main(int argc, char *argv[])
{
int ascii[256] = {0};
char str[N];
int x, y, z, i, j, k, top, top2, top3, index, index2, index3, len;
do
{
printf("String input here: \n");
fgets(str, N, stdin);
//scanf("%s", &str);
}
while (str[0] == '\n');
for(i = 0; str[i] != 0; i++)
{
++ascii[str[i]];
}
top = ascii[0];
len = strlen(str);
index = -1;
for(z = 0; str[z] != 0; z++)
{
if( ascii[str[z]] > top)
{
top = ascii[str[z]];
index = z;
}
}
if (index == -1) return printf("This string is empty\n");
else if (top == 1 && len > 1) return printf("There is no repeated character in that string\n");
else if (top == len) return printf("This string contains a single character '%c' - repeated %d times\n", str[index], top);
else {
// Checks the special case where several characters are repeated n times
char characters[len+1];
int count = 0;
for(i = 0; str[i] != 0; i++)
{
if (ascii[str[i]] == top && str[i] != characters[count-1])
characters[count++] = str[i];
}
characters[count] = 0;
if (count > 1) return printf("The most frequent characters are '%s' - repeated %d times.\n", characters, top);
else printf("The most frequent character is '%c' - repeated %d times.\n", str[index], top);
}
top2 = ascii[0];
index2 = -1;
for(j = 0; str[j] != 0; j++)
{
if( ascii[str[j]] > top2 && ascii[str[j]] < top)
{
top2 = ascii[str[j]];
index2 = j;
}
}
if (index2 == -1)
{
}
else if (top2 == 1 && len > 1)
{
}
else if (top2 == len)
{
}
else {
char characters[len+1];
int count = 0;
for(y = 0; str[y] != 0; y++)
{
if (ascii[str[y]] == top2 && ascii[str[y]] < top && str[y] != characters[count-1])
characters[count++] = str[y];
}
characters[count] = 0;
if (count > 1 )
{
}
else printf("The 2nd most frequent character is '%c' - repeated %d times.\n", str[index2], top2);
}
top3 = ascii[0];
index3 = -1;
for(k = 0; str[k] != 0; k++)
{
if( ascii[str[k]] > top3 && ascii[str[k]] < top2 && ascii[str[k]] < top)
{
top3 = ascii[str[k]];
index3 = k;
}
}
if (index3 == -1)
{
}
else if (top3 == 1 && len > 1)
{
}
else if (top3 == len)
{
}
else {
char characters[len+1];
int count = 0;
for(x = 0; str[x] != 0; x++)
{
if (ascii[str[x]] == top3 && ascii[str[x]] < top && ascii[str[x]] < top2 && str[y] != characters[count-1])
characters[count++] = str[x];
}
characters[count] = 0;
if (count > 1 )
{
}
else printf("The 3rd most frequent character is '%c' - repeated %d times.\n", str[index3], top3);
}
return 0;
}
【问题讨论】:
-
你有: for(i = 0; str[i] != 0; i++) 你的第二个 for 循环。你应该循环遍历 ascii 数组吗?
-
你可能是对的。我是编程新手。我将其更改为其他变量
-
对于输入
abcd,我认为不会有任何 3 个最常见的字符,因为所有字符出现的次数都相同。你错过了那个案子。 -
@KrishnaKanthYenumula 对,我不知道如何解决这个问题。我会在网上寻找解决方案,或者如果您有任何不介意分享的想法,这对我有很大帮助
-
看这个链接:geeksforgeeks.org/c-program-find-second-frequent-character 也有类似的问题。这会有所帮助。