Thứ Hai, 4 tháng 7, 2016

Ký tự xuất hiện nhiều nhất trong chuỗi

#include<iostream>
#include<string>
using namespace std;
void Kytuxuathiennhieunhat(char *s);
int main()
{
    char s[100];
    cout << " Hay nhap vao chuoi cua ban: ";
    cin >> s;
    cout << " Ket qua la "<<endl;
    Kytuxuathiennhieunhat(s);
    return 0;
}
void Kytuxuathiennhieunhat(char *s)
{
    int a[100], n = 0;
    for (int i = 0; i < (int)strlen(s); i++)
    {
        int d = 1;
        for (int j = i + 1; j < (int)strlen(s);j++)
        if (s[i] == s[j])
        {
            d++;
            for (int k = j; k < (int)strlen(s); k++)
                s[k] = s[k + 1];
            j--;
        }
        a[n++] = d;
    }
    int max = a[0];
    for (int i = 1; i < (int)strlen(s);i++)
    if (a[i]>max)
        max = a[i];
    for (int i = 1; i < (int)strlen(s);i++)
    if (a[i] == max)
        cout << " ky tu " << s[i] << " xuat hien nhieu nhat la " << a[i] << " lan \n";
}

Không có nhận xét nào:

Đăng nhận xét