Skip to content

Instantly share code, notes, and snippets.

@dufferzafar
Created August 22, 2013 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dufferzafar/6309278 to your computer and use it in GitHub Desktop.
Save dufferzafar/6309278 to your computer and use it in GitHub Desktop.
Frequency analysis of words in a string. C Lab Ques.
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main(void)
{
char line[250], words[25][10];
int i=0,j=0,k=0, cntr[25];
int tWords = 0;
clrscr();
printf("\nEnter a string of text :\n");
gets(line);
for(i=0;line[i] != '\0';i++)
{
if(line[i] != ' ')
{
words[j][k] = line[i];
k++;
}
else
{
words[j][k] = '\0';
j++;k=0;
}
}
words[j][k] = '\0';
tWords = j+1;
printf("\n\nWord\t\tCount\n");
for(i=0,k=0;i<tWords;i++,k++)
{
cntr[k] = 1;
for(j=i+1;j<tWords;j++)
{
if(strcmpi(words[i],words[j]) == 0)
{
words[j][0] = '\0';
cntr[k]++;
}
}
if(words[i][0] != '\0')
{
printf("\n%s\t\t%d", words[i], cntr[k]);
}
}
getch();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment