Find the maximum occurrence of a character in a string

#include<stdio.h>
#include<conio.h>

int main() {
char str[20], ch;
int count = 0, i,j=0 , max;
int arr[10];
printf("\nEnter a string : ");
scanf("%s", &str);

printf("\nEnter the character to be searched : ");
scanf("%c", &ch);
for (i = 0; str[i] != '\0'; i++) {
if (str[i] == ch)
count++;
else if(count != 0)
{
arr[j] = count;
j++;
count = 0;
}
}
arr[j]=count;
max = 0;
for(i=0;i<=j;i++)
if(max<arr[i])
max=arr[i];
if (max == 0)
printf("\nCharacter '%c'is not present", ch);
else
{
printf("\nMaximum occurrence of character '%c' : %d", ch, max);
}
return (0);
}

   

Comments

Popular posts from this blog

Write a Program to Add two 3x3 Matrix using C

C program for Unit Conversion

Write a Program to Add two 5x5 Matrix using C