Write a C program to count the 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 no of lines Step 4: Compare each character with tab char ‘\t\’ or space char ‘ ‘ to count no of words Step 5: Compare first character with NULL char ‘\0’ to find the end of 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
Program:
#include <stdio.h> #include <conio.h> #include <string.h> void main() { char str[100]; int i = 0, l = 0, f = 1; clrscr(); puts("Enter any string\n"); gets(str); for(i = 0; str[i] !='\0'; i++) { l = l + 1; } printf("The number of characters in the string are %d\n", l); for(i = 0; i <= l-1; i++) { if(str[i] == ' ') { f = f + 1; } } printf("The number of words in the string are %d", f); getch(); }
Input & Output:
Enter any string abc def ghi jkl mno pqr stu vwx yz The number of characters in the string are 34 The number of words in the string are 9
-
UpdatedDec 25, 2014
-
Views111,587
You May Like
Write a C program to find the roots of a quadratic equation
.
Write a C program to count the lines, words and characters in a given text.
Write a C program to find the sum of individual digits of a positive integer.
Write a C program, which takes two integer operands and one operator from the user, performs the operation and then prints the result. (Consider the operators +,-,*, /, % and use switch statement
)
Write a C program to generate all the prime numbers
between 1 and n, where n is a value supplied by the user.
Write C programs that use both recursive
and non-recursive functions
- To find the
factorial
of a given integer. - To find the
GCD
(greatest common divisor) of two given integers.