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

當我輸入:“ aaaaeeee”時,輸出是“ a”出現了4次,但是“ e”也出現了4次。 我知道這是按ascii值排序的,這就是為什么它給出“ a”作為輸出,但是在這種情況下,當出現這種情況時,輸出又給出了“ a”和“ e”作為輸出,我該怎么辦?

提前添加最大計算

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

請注意,您不應該使用gets原因在這里說明


整個代碼:

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

在這條線之上

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM