Find the most frequent character in the string

/*
Time limit C/C++ 3s other 6s, space limit C/C++ 32768k other 65535k

Title Description
Given a string of unlimited length, please find out the character with the most occurrence times in the string, and print out the character and its occurrence times;
If the occurrence times of multiple characters are the same, only the first character will be printed; the case format of the output character should be consistent with that of the input, and the case is not sensitive,
The case format of the output character is the same as the case format when the character first appears. It is unnecessary to consider illegal output when implementing.

Input description
The input is a string case sensitive flag, where case sensitive flag is an optional parameter and the value range is seven yuan| 1fa1 se, txue means case sensitive; the default value is txue
Example: abcdabcde fa1e

Output description
Output: number of character occurrences if there are more than one character with the most occurrences, the character with the least dictionary order will be output. The output characters are all lowercase characters
Example: a 2
*/

C++ implementation

#include<iostream>
using namespace std;


int main()
{
    char c[60000] = { 0 };
    char s[5] = { 0 };
    unsigned int count[52] = { 0 };
    char output[52] = { 0 };
    memset(c, 0, 60000);
    bool sensitive = true;
    cin >> c;
    cin >> s;
    int i = 0;
    int index = 0;
    while ((i < 5) && (s[i] != 0))
    {
        if (strcmp(&s[i + 1], "true"))
        {
            sensitive = true;
        }
        else
        {
            sensitive = false;
        }
        break;

        i++;
    }
    int j = 0;
    int max = 0, outputIndex = 0;
    while ((j < 6000)&&(c[j]!=0))
    {
        if (sensitive)
        {
            if (90 < c[j])
            {
                index = c[j] - 'A';
                count[index]++;
                if (output[index] == 0)
                {
                    output[index] = c[j];
                }
                if (max < count[index])
                {
                    max = count[index];
                    outputIndex = index;
                }
            }
            else
            {
                index = c[j] - 'a';
                count[index + 26]++;
                if (output[index + 26] == 0)
                {
                    output[index + 26] = c[j];
                }
                if (max < count[index + 26])
                {
                    max = count[index + 26];
                    outputIndex = index + 26;
                }
            }
        }
        else
        {
            if (90 < c[j])
            {
                index = c[j] - 'A';
            }
            else
            {
                index = c[j] - 'a';
            }
            count[index]++;
            if (output[index] == 0)
            {
                output[index] = c[j];
            }
            if (max < count[index])
            {
                max = count[index];
                outputIndex = index;
            }
        }
        j++;
    }
    cout << output[outputIndex] << " " << max << endl;
    system("pause");
    return 0;
}

 

Note compiler incompatibility

Keywords: C++

Added by ClevaTreva on Sat, 04 Apr 2020 03:34:10 +0300