Frequency of Character in a String - Learn Engineering

All the news and updates relevant to Engineering. You can also Write a post here

Breaking

Friday, June 26, 2020

Frequency of Character in a String

C++ PROGRAM TO FIND THE FREQUENCY OF CHARACTER IN A STRING

Sample Test Case    :    Enter the String : Hello

Sample Output    :  H - 1

                                  e - 1

                                  l - 2

                                  o -  1 

CODE

#include <iostream>
#include<bits/stdc++.h>
using namespace std;

int main()
{
    char str[100];
    int i;
    int freq[256]={0};
    cin>>str;
    for(i=0;str[i]!='\0';i++)
    {
        freq[str[i]]++;
    }
    for(i=0;i<256;i++)
    {
        if(freq[i]!=0)
        {
            cout<<(char)i<<" "<<freq[i]<<endl;
        }
    }

    return 0;
}

No comments:

Post a Comment