Name the files bonusc and bonush char highestFreqchar str th

Name the files, bonus.c and bonus.h char highestFreq(char *str) this function should return the highest frequency character c in the string. For example, a call to this function with the string \"Hello World\", would return \"l\".

Solution

//bonus.c
#include <stdio.h>
#include \"bonus.h\"

int main()
{
   char * str=\"Hello World\";
   char c=highestFreq(str);
   printf(\"Highest frequency character is %c\", c);
   return 0;
}

///////////////////////////////////////////////////////////////////////

//bonus.h

#include <string.h>
char highestFreq(char*str)
{
   const int len=strlen(str);
   int freq[len],i,k;

   i=0;
while(str[i] != \'\\0\')
{
k = (int)str[i];
freq[k] += 1;
i++;
}
int max = freq[str[0]]; // Initialize max count
char ans=str[0]; // Initialize result
for (i = 1; i < len; i++) {
if ( freq[str[i]]>max) {
max = freq[str[i]];
ans = str[i];
}
}
return ans;  
}

 Name the files, bonus.c and bonus.h char highestFreq(char *str) this function should return the highest frequency character c in the string. For example, a cal

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site