Write a C program to find the most frequent character in the input string, count how many times the most frequent charac
Posted: Sun May 15, 2022 1:45 pm
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;
}
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;
}