简体   繁体   English

字符数组中的最大字符数

[英]Maximum number of Characters in a character array

//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');
}

When I give it the input: "aaaaeeee", the output is "a" occurring 4 times, but "e" occurs 4 times too. 当我输入:“ aaaaeeee”时,输出是“ a”出现了4次,但是“ e”也出现了4次。 I know this is sorted by ascii values and thats why it gives "a" as output, but what can I do in this program that the output gives both "a" and "e" as output when a case like this occurs? 我知道这是按ascii值排序的,这就是为什么它给出“ a”作为输出,但是在这种情况下,当出现这种情况时,输出又给出了“ a”和“ e”作为输出,我该怎么办?

Add max calculation ahead 提前添加最大计算

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

Note that this is the max number of the same character you might have. 请注意,这是您可能拥有的相同字符的最大数量。

Then display all chars which maximum is equal to max 然后显示所有等于最大的字符

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

To fix the endless loop, before the while add char c ; while ((c = getchar()) != EOF && c != '\\n'); 要修复无限循环,请在while之前添加char c ; while ((c = getchar()) != EOF && c != '\\n'); char c ; while ((c = getchar()) != EOF && c != '\\n');

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

Note that you shouldn't use gets , reason is explained here . 请注意,您不应该使用gets原因在这里说明


Whole code: 整个代码:

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');
}

Above this line 在这条线之上

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

Delete it and add this code 删除并添加此代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM