Counting of charcaters, words and lines in a text

Program 39) One of the practical applications of string manipulations is counting the words in a text. We assume that a word is a sequence of any characters, except escape characters and blanks, and that two words are separated by one blank character. The algorithm for counting words is as follows:
   1. Read  a line of text.
   2. Beginning from the first character in the line, look for  
       blank. If a blank found, increment words by 1.
   3. Continue steps 1 and 2 until the last line is completed.

The implementation of this algorithm is shown below. The first while loop will be executed once for each line of text. The end of text is indicated by pressing the 'Return' Key an extra time after the entire text has been  entered. The extra 'Return' Key causes a newline character as input to the last line and as a result, the last line contains only the null character.
The program checks for this special line using the test
                                      if (line [0] == '\0')
and if the first (and only the first) character in the line is a null character, then counting is terminated. Note the difference between a null character and a blank character.

                                  PROGRAM

#include <stdio.h>
main ( )
 {
      char line [81], ctr ;
      int i, c, end = 0, words = 0, lines = 0 ;
      printf ("KEY IN THE TEXT. \n") ;
      printf ("GIVE ONE SPACE AFTER EACH WORD. \n") ;
      printf ("WHEN COMPLETED, PRESS 'RETURN' .\n\n") ;
      while (end == 0) 
       {
              /*  Reading a line of text  */
              c = 0 ;
             while ( (ctr = getchar() )  ! =   '\n') ;
                      line [c++] = ctr ;
             line [c] = '\0' ;
            /*  counting the words in a line  */
            if (line [0] == '\0')
               break ;
           else
           {
               words++ ;
               for (i = 0; line[i]  !=   '\0'; i++)
                          if (line [i] == ' '  | | line [i] == '\t') 
                             words++ ;
           }
           /*  Counting lines and characters  */
           lines = lines + 1 ;
           characters = characters + strlen(line) ;
       }
       printf ("\n") ;
       printf ("Number of lines = %d\n", lines) ;
       printf ("Number of words = %d\n", words) ;
       printf ("Number of characters = %d\n", charcaters) ;
 }


Output:     KEY IN THE TEXT
                  GIVE ONE SPACE AFTER EACH WORD.
                  WHEN COMPLETD, PRESS 'RETURN'.


             Admiration is a very short-lived passion.
             Admiration involves a glorious obliquity of vision.
            Always we like those who admire us but we do not
            Like those whom we admire.
            Fools admire, but men of sense approve.


            Number of lines = 5
           Number of words = 36
           Number of characters = 205


The program also counts the number of lines read and the total number of characters in the text. Remember, the last line containing the null string is not counted.
After the first while loop is exited, the program prints the result of counting.
         

No comments:

Post a Comment