C Program to Count Lines Words and Characters in a Given Text

Write a C program to count lines words and characters in a given text or string. In this program, we are reading text or string data from the keyboard but you can read it from a file using file handling, or you can read it from different places. Write reading of those strings accordingly, here we will mainly focus on how to count characters, words, and lines from a given text data.

Important points to count characters, words, and lines,

  • The lines always ended with a new-line character (‘\n’).
  • The words can be ended with space or tab. And it also completed when a new-line character appears i.e. whenever one line is completed then one word is also completed.

Procedure to develop the program,

1) Read the string. (Since it may contain multiple lines so write logic accordingly).
2) Take three variables to count line=0, word=0, ch=0.
3) Read characters one by one until the end.
4) If it is a new line character (‘\n’) then one line is completed, and one word is also completed. Increase line and word variables.
5) If it is a space or tab then one word one character is completed. Update those variables.
6) Else it is a normal character, update only the ch variable.
7) Finally display the count variables value.

#include<stdio.h>
int main()
{
   // declare variables
   char str[200];
   int line, word, ch;

   // initialize count variables with zero
   line = word = ch = 0;

   // read multiline string
   printf("Enter string terminated with ~ :\n");
   scanf("%[^~]", str);

   // check every character
   for(int i=0; str[i]!='\0'; i++)
   {
      // if it is new line then
      // one line and one word completed
      if(str[i]=='\n')
      {
         line++;
         word++;
      }

      // else it is a character
      else 
      {
         // if character is space or tab
         // then one word is also completed
         if(str[i]==' '||str[i]=='\t')
         {
            word++;
            ch++;
         }

         // it was not '\n', sapace or tab
         // it is a normal character
         else {
            ch++;
         }
      }
   }

   // display count values
   printf("\nCharacter counts = %d\n", ch);
   printf("Word counts = %d\n", word);
   printf("Line counts = %d\n", line);

   return 0;
}

Output:-

Enter string terminated with ~ :
Hello, how are you?
Welcome to the programming world.
Programming is fun.
~
Character counts = 71
Word counts = 12
Line counts = 3

In this C program to count lines words and characters in a given text or String, we take input from the user by terminated tilde(~). We use this because the user can enter more than one line. Lines are terminated by ‘\n’. So, the program takes input (only up to 200 sizes) until the user not entering ~. When the program encounter ~ then it will stop reading from the standard input. There are different ways to read multiline strings, see here.

Using for loop and nested if-else every character is checked. If the character is space or tab then previously there was a word. Similarly, a line is terminated with ‘\n’. Finally for the given inputs we got character counts = 71, Word counts = 12 and Line counts = 3.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *