【问题标题】:Maximum number of Characters in a character array字符数组中的最大字符数
【发布时间】:2017-11-21 08:52:31
【问题描述】:
//Program to find max occurring character in string

#include<stdio.h>
#include<conio.h>
#define MAX_SIZE 100  // Maximum string size, change to make string smaller or larger
#define MAX_CHARS 255 // Maximum characters allowed for characters


void main()
{
    char str[MAX_SIZE];  //store the string
    int freq[MAX_CHARS]; // store frequency of each character
    int i, max; // i is for loop max to store frequency
    int ascii;   //stores ascii value convertd from each char
    char ch;    //for choice

    do{
        clrscr();
        i=0;
    printf("\nEnter any string: ");
    gets(str);

    // Initializes frequency of all characters to 0
    for(i=0; i<MAX_CHARS; i++)
    {
    freq[i] = 0;
    }


    // Finds occurance/frequency of each characters
    i=0;
    while(str[i] != '\0')
    {
    ascii = (int)str[i];
    freq[ascii] += 1;       //string's element is casted to int to store its ascii value for further comparision

    i++;
    }


    // Finds maximum frequency of character
    max = 0;
    for(i=0; i<MAX_CHARS; i++)
    {
    if(freq[i] > freq[max])
        max = i;            //to print no. of times 
    }


    printf("\nMaximum occurring character is '%c' = %d times.", max, freq[max]);
    printf("\n Want to find again??(y/n):");
    scanf("%c",&ch);
    }while(ch=='Y'||ch=='y');
}

当我给它输入:“aaaaeeee”时,输出是“a”出现 4 次,但“e”也出现 4 次。我知道这是按 ascii 值排序的,这就是为什么它给出“a”作为输出,但是当这种情况发生时,我能在这个程序中做什么,输出同时给出“a”和“e”作为输出?

【问题讨论】:

  • 旁注:不要使用 void main(),使用 int main()..以及 conio.h 标头。
  • 我是编码新手,有什么区别?
  • 请过这个问题,这里详细讨论了main的返回类型:stackoverflow.com/questions/204476/…
  • 永远,永远,永远不要使用gets,使用fgets(或POSIX getlinegets是如此不安全且易受缓冲区溢出的影响,它已从C11中删除图书馆。扔了它。如果你的教授想让你使用它,也扔给他。
  • 这不是大学工作或任何东西,它是出于我自己学习编码的好奇心,我会照顾它的。

标签: c


【解决方案1】:

提前添加最大计算

 i = 0;
 max = 0;
 while(str[i] != '\0')
 {
    ascii = (int)str[i];
    freq[ascii] += 1;
    if (freq[ascii] > max) max = freq[ascii]; // <==== here
    i++;
 }

请注意,这是您可能拥有的相同字符的最大数量。

然后显示最大值等于最大值的所有字符

for(i=0; i<MAX_CHARS; i++)
{ 
   if(freq[i] == max) printf("Character %c is at max %d\n", i, max);
}

要修复无限循环,请在 while 之前添加 char c ; while ((c = getchar()) != EOF &amp;&amp; c != '\n');

   scanf("%c",&ch);
   char c;
   while ((c = getchar()) != EOF && c != '\n'); // <== note the ';'
} while(ch=='Y'||ch=='y');

请注意,您不应使用getsreason is explained here


完整代码:

void main()
{
    char str[MAX_SIZE];  //store the string
    int freq[MAX_CHARS]; // store frequency of each character
    int i, max; // i is for loop max to store frequency
    int ascii;   //stores ascii value convertd from each char
    char ch;    //for choice

    do {
        printf("\nEnter any string: ");
        gets(str);

        // Initializes frequency of all characters to 0
        for(i=0; i<MAX_CHARS; i++)
        {
            freq[i] = 0;
        }


        // Finds occurance/frequency of each characters
        for(i=0,max=0 ; str[i] != '\0' ; i++)
        {
            ascii = (int)str[i];
            freq[ascii] += 1;       //string's element is casted to int to store its ascii value for further comparision
            if (freq[ascii] > max) max = freq[ascii];
        }

        for(i=0; i<MAX_CHARS; i++)
        { 
            if(freq[i] == max) printf("Character %c is at max %d\n", i, max);
        }

        printf("\n Want to find again??(y/n):");
        scanf("%c",&ch);
        char c;
        while ((c = getchar()) != EOF && c != '\n'); 
    }while(ch=='Y'||ch=='y');
}

【讨论】:

  • 只有一个问题,当我通过键入“y”重新运行代码时,当提示我是否要再次查找时,当时输出显示 ASCII 中的每个字符,并且不占用输入
  • stackoverflow.com/questions/24073138/…。基本上在结尾while前加char c ; while ((c = getchar()) != EOF &amp;&amp; c != '\n');
  • 代码重新运行后输出错误,即使没有重新运行,在某些情况下也会出现错误,例如“aaaa”输出为“字符a最多为2”
  • 当然 i++ 必须在“完整代码”中删除 - 我将 i=0 ; while ... 更改为 for ... 因此 i 增加了两次。代码已编辑!
【解决方案2】:

在这条线上

printf("\nMaximum occurring character is '%c' = %d times.", max, freq[max]);

删除它并添加此代码

for(i=0;i<MAX_CHARS;i++)
{
    if(freq[i]==freq[max])
    {
        printf("\nMaximum occurring character is '%c' = %d times.", i, freq[i]);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多