Q)Write the C++ program for character frequency count for any sentence
=> Program :
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
int main(){
string mystr;
cout<<"Enter the Any Sentence for character frequency count \n";
getline(cin,mystr);
int charFreq[26]={0};
for(int i=0;i<mystr.length();i++){
int index=mystr[i]-'a';
charFreq[index]++;
}
cout<<"\n****************************************\n";
cout<<"\nSentence : "<<mystr<<endl<<endl;
cout<<setw(12)<<"Character"<<setw(12)<<"Frequency"<<endl<<endl;
for(int i=0;i<26;i++){
if(charFreq[i]!=0){
cout<<setw(6)<<char(i+'a')<<setw(10)<<charFreq[i]<<" times \n";
}
}
}
=>Output :
Comments
Post a Comment