Wednesday 15 May 2013

Counting number of letters and words in a text in c programing -


I try to write a program that counts the words and letters in a text taken from the file in the hard drive. But this program only counts the characters of the first line.What can I do? Please give your thoughts about this program. Please debug your code in your compiler and help me improve it.

  # include & lt; Stdio.h & gt; #to & lt include, conio.h & gt; #to & lt include, string.h & gt; # Include & lt; Ctype.h & gt; Int Liter (Constant Charles []); Int word (Const four sentences []); Int main (int argc, char * argv []) {character sentence [100]; Const char * filename = "test.txt"; FILE * cfPtr; If (argc == 2) filename = argv [1]; If ((cfPtr = fopen (filename, "r")) == faucet {printf ("The file '% s' could not be opened \ n", file name); } Else {int total = 0; Int Total 2 = 0; While (fgets (sentence, sentence sentence, cfPtr)) Total + = words (sentences); Total 2 + = letters (sentences); Printf ("% d \ n", total 2); Printf ("% d \ n", total); Printf ("****% s", * cfPtr); Fclose (cfPtr); } Getch (); Return 0; } Int word (const char sentence []) {int i, length = 0, count = 0, last = 0; Length = Stellen (sentence); (I = 0; i & lt; length; i ++) {if (sentence [i] == '' || sentence [i] == '\ t' || sentence [i] == '\ n' ) Count ++; } Calculation of return; } Inter letters (Constant Charles sentence []) {int i, length = 0, calculation = 0; Length = Stellen (sentence); (I = 0; i & lt; length; i ++) {if ((sentence [i] & gt; 'a' & [i] & gt; 'z'); (Sentence [i] & gt; ; A '& amp; Amp; amp; Sentence [i] & gt;' Z ') count ++; } Calculation of return; }    

while you are closing the file in () Loop you want to get it out of

  while (fgets (sentence, sentence sentence, cfPtr)) Total + = word (sentence); Total 2 + = letters (sentences); Printf ("% d \ n", total 2); Printf ("% d \ n", total); Printf ("****% s", * cfPtr); Fclose (cfPtr); // & lt; ------- You want to loop it out}    

No comments:

Post a Comment