Computer Science‎ > ‎

C Program: Displaying a Histogram of word frequencies (unigram)

C program to display the total number of words,the number of unique words and the frequency of each word


/* The following C program displays the total number of words,the number of unique words and the frequency of each word
The program uses simple implementaion of sscanf to read from a charcater array text and scans for the frequency of word
*/
#include<stdio.h>
#include<string.h>
char *text;
int main()
{
text=(char *)malloc(100*sizeof(char));//The text array stores the entered string by the user and we assume max text size is 99
char *word=(char *)malloc(10*sizeof(char));//We create 2 more arrays word and temp to store the wors ands we assume max word size is 10
char *temp=(char *)malloc(10*sizeof(char));
printf("\nEnter the text of max length 99 and max length of any word should be 10\n");
gets(text);//use gets to input the line of text
int len=0;//works as counter variable to move through text
int words=0;//store total number of words
int unique=0;//count unique wors
while(len<strlen(text))
{
words++;
sscanf(text+len,"%s",word);//scanf from appropriate length
int ctr=0;//gives correct scanning poistion
int freq=0;//set frequency zero
while(ctr<strlen(text))
{//again scan from the start
sscanf(text+ctr,"%s",temp);
if(strcmp(word,temp)==0)//compare the words
freq++;//increment
ctr=ctr+strlen(temp)+1;//scan the next word so sum up the length for previous word and 1 for the space
}
if(freq!=1)
printf("\n<%s> is not unique and has frequency %d\n",word,freq);//the word has frequency more than 1
else
{
    printf("\nThe word <%s> is unique\n",word);//unique
    unique++;
}
len=len+strlen(word)+1;//scan the next word so sum up the length for previous word and 1 for the space
}
printf("\nThe number of unique words is %d\n",unique);
printf("\nThe total number of words are %d\n",words);
getch();
}
/*A sample run of the program works as:-
Enter the text of max length 99 and max length of any word should be 10
The Learning Point
The word <The> is unique
The word <Learning> is unique
The word <Point> is unique
The number of unique words is 3
The total number of words are 3
Enter the text of max length 99 and max length of any word should be 10
the learning point is the best
<the> is not unique and has frequency 2
The word <learning> is unique
The word <point> is unique
The word <is> is unique
<the> is not unique and has frequency 2
The word <best> is unique
The number of unique words is 4
The total number of words are 6
*/




 


C Program to Reverse A String

C Program: Building an Expression Evaluator
C Program: Check for Armstrong Numbers
C Program: Check whether a string is a Palindrome or not
C Program: Common Operations on Sets - Union, Intersection, Difference, Cardinal Product
C Program: Computing exp(x), sin(x), cos(x), tan(x) using series expansions
C Program: Computing the Area of a Circle
C Program: Computing the Upper Triangular Matrix and Lower Triangular Matrix
C Program: Demonstrating File Handling Functions
C Program: Demonstrating Operations on Matrices - Addition, Subtraction, Multiplication, Inversion, Finding Determinants
C Program: Demonstrating the use of Bitwise Operators
C Program: Displaying a Histogram of word frequencies (unigram)
C Program: Distance Vector Routing Algorithm using Bellman Ford's Algorithm
C Program: Numerical Computing - The Gaussian Elimination Method
C Program: Numerical Computing - Implementing the Newton Raphson Method
C Program: Numerical Computing - the Bisection Method
C Program: Numerical Computing - The Gaussian Elimination Technique from Linear Algebra
C Program: Numerical Computing - the Jacobi Method
C Program: Printing the Pascal Triangle
C Program: Reversing the order of words in a sentence
C Program: Solving Simultaneous Equations in Two Variables
C Program: Source Code for computing the GCD(HFC) of two numbers
C Program: Source Code for Solving Quadratic Equations
C Program: Source code to solve the Josephus Problem
C Program: Sudoku Solver
C Program: The Usage of Command Line Arguments
C Program: Using the Sieve of Eratosthenes to print Prime Numbers