Skip to content

Instantly share code, notes, and snippets.

@sadik2702
Created September 26, 2022 17:10
Show Gist options
  • Save sadik2702/cc8f9f4f220e298e5ecc6412c350274e to your computer and use it in GitHub Desktop.
Save sadik2702/cc8f9f4f220e298e5ecc6412c350274e to your computer and use it in GitHub Desktop.
Find the most frequent word from a sentence using C
#include <stdio.h>
#include <string.h>
int main()
{
char a[100], b[100][100];
int i, j = 0, k = 0, l, x, w1 = 0, w2 = 0, w3 = 0;
printf("Enter a String:\n");
gets(a);
l = strlen(a);
for (i = 0; i < l; i++)
{
if (a[i] != ' ')
{
w1++;
while (a[i] != ' ' && a[i] != '\0')
{
b[j][k] = a[i];
k++;
i++;
}
}
j++;
k = 0;
}
for (i = 0; i < w1; i++)
{
for (j = 0; j < w1; j++)
{
if (strcmp(b[i], b[j]) == 0)
{
w2++;
}
}
if (w2 > w3)
{
x = i;
w3 = w2;
}
w2 = 0;
}
printf("Max Frequency: %d\n", w3);
printf("Frequent word: %s\n", b[x]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment