C++: Write a function that receives a pointer to a character string consisting of only alphabets a-z and returns the character with the maximum number of repetitions. If more than one-character repeats same number of times, return the character with smallest alphabetical order. For example, the string “Mississippi” has three repeated characters (i-4, s-4, p-2). Both ‘i’ and ‘s’ repeated 4 times but ‘i’ is alphabetically smaller hence the function should return ‘i’. Do not count repeated blanks, special characters, capitalization, spaces, punctuations in the string.. Assume that the function has the prototype statement. char mostRepeatedChar(char *ptr); The main file that allows the user to enter a character string, and then tests this function.
#include<iostream>
using namespace std;
char mostRepeatedChar(char *ptr){
//counter to store the frquency of each character at their ascii value
// Like A-65 B-66 etc
int counter[123]={0};
for(int i=0;ptr[i]!='\0';i++){
char c= ptr[i];
//storing only for chars
if( (c>='A' && c<='Z') or (c>='a' && c<='z') ){
counter[c]++;
}
}
int max=0;
//finding the index of max value in the array
for(int i=0;i<123;i++){
//ignore 0 counts
if(counter[i]==0)
continue;
if(counter[max]<counter[i])
max=i;
}
//converting max index to char
return (char)max;
}
int main(){
char arr[100];
cout<<"Enter string: ";
cin>>arr;
cout<<mostRepeatedChar(arr);
//Mississippi
}
NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.
Please Like and Support me as it helps me a lot
Get Answers For Free
Most questions answered within 1 hours.