Zliczanie najczęstszej liczby znaków

0

#include <iostream>
#include <string>
#include <algorithm>

int main()
{
using namespace std;
string s;
cout << "Podaj tekst:" << endl;
getline(cin, s); //wyswietlenie tekstu w jednej lini

for (char i = 'a'; i <= 'z'; i++) // od a do z
{
int c = count(s.begin(), s.end(), i);
if (c != 0)
{
cout << i << " " << c << endl;
}
}
}

Mam coś takiego ale nie wiem jak sprawić aby na końcu pokazywalo litere ktora wystapila najwiecej razy

1

Podpowiedź - użyj mapy która automatycznie sortuje elementy

 map<int,char> counts;
 auto c = count(s.begin(), s.end(), i);
 counts[ c ] = i;

......

counts.rbegin(); // tutaj będziesz miał maksymalną wartość
0

Nie zbyt rozumiem jak używać mapy bo dopiero zaczynam ;/

1

To w takim razie wersja toporna:

#include <iostream>

using namespace std;

int alpha[26] = {0};

int main(void)
{
    string text;

    cout << "Enter text:" << endl;
    getline(cin, text);

    for (int i = 0; i < text.length(); i++)
    {
        int a = text[i];

        if (a >= 'A' && a <= 'Z')
        {
            alpha[a - 'A']++;
        }
        else if (a >= 'a' && a <= 'z')
        {
            alpha[a - 'a']++;
        }
    }

    cout << "Max frequent:" << endl;
    int maximum = 0;
    char max_char;

    for (int i = 0; i < 26; i++)
    {
        if (alpha[i])
        {
            if (alpha[i] >= maximum) {
				maximum = alpha[i];
				max_char = char('a' + i);
			}
 				
				
        }
    }
    cout << max_char<<"\n";
    return 0;
}

Źródło: https://stackoverflow.com/questions/38547564/c-program-to-check-the-frequencies-of-the-letters

1 użytkowników online, w tym zalogowanych: 0, gości: 1