Write a C program to count the number of lines, words and characters in a given text

In this article, we will write a C program to count the number of lines, words, and characters in a given text.

ALGORITHM: 

Step 1: Start 

Step 2: Read the text until an empty line 

Step 3: Compare each character with newline char ‘\n’ to count the of lines 

Step 4: Compare each character with tab char ‘\t\’ or space char ‘ ‘ to count the of words 

Step 5: Compare the first character with NULL char ‘\0’ to find the end of a text 

Step 6: No of characters = length of each line of text 

Step 7: Print no of lines, no of words, no of chars 

Step 8: Stop. 

Write a C program to count the number of lines, words, and characters in a given text

#include <stdio.h>

void main()
{
	char line[81], ctr;
	int i, c,
	end = 0,
		characters = 0,
		words = 0,
		lines = 0;
	printf("TYPE ANY TEXT.\n");
	printf("GIVE ONE SPACE AFTER EACH WORD.\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", characters);
}

INPUT:

TYPE ANY TEXT.
GIVE ONE SPACE AFTER EACH WORD.
c programming source code examples

OUTPUT:

Number of lines = 1
Number of words = 5
Number of characters = 34

Related C Programs with Output

  1. Write a C Program to Find the Sum and Average of Three Numbers
  2. Write a C Program to Find the Sum of Individual Digits of Positive Integer
  3. Write a C Program to Generate the First N Terms of the Sequence
  4. Write a C Program to Generate All Prime Numbers Between 1 and N
  5. Write a C Program to Check Whether Given Number Is Armstrong Number or Not
  6. Write a C program to evaluate algebraic expression (ax+b)/(ax-b)
  7. Write a C program to check whether a given number is a perfect number or Not
  8. Write a C program to check whether a number is strong number or not
  9. Write a C program to find the roots of a quadratic equation
  10. Write a C program to find the factorial of a given integer using a non-recursive function
  11. Write a C program to find the factorial of a given integer using a recursive function
  12. Write a C program to find the GCD of two given integers by using the recursive function
  13. Write a C program to find the GCD of two given integers using a non-recursive function
  14. Write a C program to find both the largest and smallest number in a list of integers
  15. Write a C Program to Sort the Array in an Ascending Order
  16. Write a C Program to find whether the given matrix is symmetric or not
  17. Write a C program to perform the addition of two matrices
  18. Write a C Program That Uses Functions to Perform Multiplication Of Two Matrices
  19. Write a C program to use a function to insert a sub-string in to a given main string from a given position
  20. To delete n Characters from a given position in a given string
  21. Write a C program using user-defined functions to determine whether the given string is palindrome or not
  22. Write a C program to count the number of lines, words, and characters in a given text
  23. Write a C program to find the length of the string using Pointer
  24. Write a C program to Display array elements using calloc( ) function
  25. Write a C Program to Calculate Total and Percentage Marks of a Student Using Structure
  26. Write a C Program to Display the Contents of a File
  27. Write a C program to copy the contents of one file to another

Comments