Write a C program to find the most frequent character in the input string, count how many times the most frequent charac

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

Write a C program to find the most frequent character in the input string, count how many times the most frequent charac

Post by answerhappygod »

Write a C program to find the most frequent character in the
input string, count how many times the most frequent character
occurred. Print the result.
If more than one character has the same occurring frequency,
print the first one.
I can't get the code to print only the first most frequent
character. please help modify this code.
#include <stdio.h>
#include <string.h>
int main(int argc,
const char * argv[]) {
char str[100];
printf("Enter any string: ");
scanf("%s", str);
int length;
length = strlen(str);
int temp[100], i, j, k = 0, count
= 0;
for(i = 0; i < length;
i++){
temp = 0;
count = 1;
if(str){
for(j
= i + 1; j < length; j++){
if(str == str[j]){
count++;
str[j] = '\0';
}
}
}
temp = count;
if(count >=
k){
k = count;
}
}
for(j = 0; j < length;
j++){
if(temp[j] ==
k){
printf("Maximum
occuring character is '%c', ", str[j]);
}
}
printf("occured %d times.\n", k);
return 0;
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply