Created
August 22, 2013 16:07
-
-
Save dufferzafar/6309278 to your computer and use it in GitHub Desktop.
Frequency analysis of words in a string. C Lab Ques.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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