Write a C program to count the lines, words and characters in a given text
/* Write a C program to count the lines, words and characters in a given text*/
char line, ctr;
end = 0,
characters = 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')
for(i=0; line[i] != '\0';i++)
if(line[i] == ' ' || line[i] == '\t')
/* counting lines and characters */
lines = lines +1;
characters = characters + strlen(line);
printf("Number of lines = %d\n", lines);
printf("Number of words = %d\n", words);
printf("Number of characters = %d\n", characters);
KEY IN THE TEXT.
GIVE ONE SPACE AFTER EACH WORD.
WHEN COMPLETED, 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
Write a C program to count the lines, words and characters in a given text Reviewed by Rejin Paul on 10:25 AM Rating: