【问题标题】:C language - print the 3 most frequent characters from user input string [closed]C语言-从用户输入字符串中打印3个最常见的字符[关闭]
【发布时间】: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 也有类似的问题。这会有所帮助。

标签: c loops ascii c-strings


【解决方案1】:

你可以做这样的事情并使用类似的逻辑来处理第二种和第三种情况:

    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];
        memset(character, 0, len+1);
        int count = 0;
        for(i = 0; str[i] != 0; i++)
        {
            if (ascii[str[i]] == top && !strchr(characters, str[i])) 
                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);
    }

【讨论】:

  • 我尝试按照这个方法,但是因为“ld返回1退出状态”而无法编译[pastebin.com/kA9Bch1B].我今年 10 月开始上编程课,不太了解如何将想法应用到程序中
  • 上面的代码只是一个片段,您必须将其粘贴到您的主函数中以替换从index=0; 开始并通过第一个printfstatement(包括)的表单。我测试了它,它应该可以工作。我想帮助您解决这个问题,但很遗憾,您在评论中提供的链接不包含任何代码。
  • 我是否应该将 vars len 和 memset 都设为 int 和 = 0 并为每个下一个频繁的 char 制作例如len2、memset2、characters2 等?
  • len 是字符串的长度(对不起,我忘了在我的代码中显示它)所以,不,它在 3 种情况下保持不变。 memset 不是 var,而是一个用零值初始化数组的函数。但实际上这并不是绝对必要的,因为我稍后再次设置了空终止:所以我删除了它。无论如何,所有以else { 开头并到结尾的形式都在另一个范围内(在大括号内),对于其他语句不可见,因此即使您在下一个案例中重用它们,您也不必担心 var 名称。
  • 此时我只剩下 1 个问题:如果我的输入是“qwqwqwqwdcdcdcxzz”程序,它将打印“最常见的字符是 'qwqwqwqw' - 重复 4 次。”我不知道如何让它说“qw 4 times”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多