here is the program which is supposed to find the most common element in a string. But it crashes when I enter a string.
#include <stdio.h>
#include <conio.h>
#include <string.h>
int main(){
char a[100];
scanf("%s", a);
int max=0,n,k;
int urt = strlen(a);
for(int i=0; i<urt-1; i++){
n=0;
for(int l=i+1; l<urt; l++){
if(a[i]==a[l]) n++;
}
if(max<n){
max=n;
k=i;
}
}
printf("%s\n", a[k]);
printf("%d", max);
getch();
return 0;
}
Which means you need to change:
to:
because
a[k]is not a string, but a character. Also to get a more accurate output, add 1 to n to consider the character you are scanning (from the outer loop):