Question 6: Write a C++ program that takes a string as input and display character(s) with highest occurrence in a String.
Question 6: Write a C++ program that takes a string as input and display character(s) with highest occurrence in a String.
/*
Name: Mudassar Ali
Roll: BCSF15M045 [Morning]
Assignment: No# 05
Teacher: Miss Sadia Shahzad
*/
#include <iostream>
using namespace std;
int main()
{
cout<<"\tThis Programme will show the character with highest occurrence!!\n\n";
char str[50]; // To hold the content of string.
int count[26]={0}; // To hold the number of iteration values for string.
char ch[26]; // String to store characters.
int p=1;
char ch1='a' ; // char for characters.
cout<<"Enter the String: ";// to store the result from string.
cin>>str; // Inputting the string.
for(int c=0;c<=25;c++) // loop to store the the alphabets characters.
ch[c]=ch1++; // storing characters lower case.
for(int i = 0; str[i]!='\0';i++) // loop to count the number of words separately.
{
if(str[i]>='a'&& str[i]<='z')
{
count[str[i]-'a']++; // counter ++ the index..
}
}
cout<<"\nCharacter (s) with higher occurrence: ";
for(int i = 0; i<26;i++) // loop to compare with the inner loop count string.
{
p = 1;
for(int j = 0;j<26&& p==1;j++)
{
if(count[i]<count[j])
{
p = 0;
}
}// end of inner loop.
if(p) // if p have p=1 then character will print with higher occurrences.
cout<<ch[i]<<" ";
}// end of outer loop
cout<<endl;
return 0;
}
No comments: