Text analyzer

Text analyzer


The following Text analyzer program is implemented in c program. It program analyzes the given text from the user and calculate the number of words and total number of characters in the line of text. It determines the numbers of vowels, consonants, white space characters and other characters for each line. And finally determines average number of vowels, consonants per line.

Concepts used:

Array concept is used to store the input text which is to be analysed. The input text is entered during run time. The text contains space hence gets function is used to get the input text from the user.
Local variables and global variables are used. For loops and while loops are used to calculate the number of characters and words. Function is used to check whether the given character matches with the available character.

Source code:

 #include<stdio.h>  
 #include<conio.h>  
 #include<string.h>  
 int stc(char *,char);  
 void main()  
 {  
 int ch,ln=0,space,vow,con,other,err,tch=0,twrd=0,tvow=0,tcon=0;  
 char txt[500],*l, v[]="aeiouAEIOU";  
 char c[]="bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ";  
 printf("\t\t\t\tTEXT ANALYZER");  
 printf("\nEnter the text to be analyzed :\n");  
 gets(txt);  
 for(l=strtok(txt,".");l!=NULL;l=strtok(NULL,"."))  
 {  
 ch=1,err=0,vow=0,space=0,other=1,con=0;  
 ln++;  
 if(*l==' ')  
 {  
 err++;  
 }  
 while(*l!='\0')  
 {  
 if(stc(v,*l)==1)  
 vow++;  
 else if(stc(c,*l)==1)  
 con++;  
 else if(*l==' ')  
 space++;  
 else  
 other++;  
 l++,ch++;  
 }  
 if(*(l-1)==' ')  
 {  
 err++;  
 }  
 printf("\nnumber of vowels in line %d : %d" ,ln,vow);  
 printf("\nnumber of consonants in line %d : %d" ,ln,con);  
 printf("\nnumber of white spaces in line %d : %d" ,ln,space);  
 printf("\nnumber of other characters in line %d : %d" ,ln,other);  
 printf("\nnumber of words in line %d : %d" ,ln,space+1-err);  
 printf("\nnumber of characters in line %d : %d\n" ,ln,ch);  
 tvow+=vow;  
 tcon+=con;  
 tch+=ch;  
 twrd+=space+1-err;  
 }  
 printf("\nAverage number of vowels per line : %d",tvow/ln);  
 printf("\nAverage number of consonants per line : %d",tcon/ln);  
 printf("\nTotal number of words : %d",twrd);  
 printf("\nTotal number of characters : %d",tch);  
 getch();  
 }  
 int stc(char *str, char c)  
 {  
 while(*str!='\0')  
 {  
 if(*str==c)  
 return 1;  
 str++;  
 }  
 return 0;  
 }  

OUTPUT:

Text analyser sample output
Text analyzer

text analyzer sample output
Text analyzer

1 comment :

  1. Hai, i need the same text analyzer c program using files concept....

    ReplyDelete