Subscribe

RSS Feed (xml)

Monday, January 26, 2009

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

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
#include(ctype.h)
int main( )
{
char ch;
int line=0, space=0, ct=0;
clrscr( );
printf("Enter the required no. of lines:\n");
while((ch=getchar( ))!=EOF)
{
if(ch==10) { line++; }
if(isspace(ch)) { space++; }
ct++;
}
printf("\nNumber of Lines : %d", line+1);
printf("\nNumber of Words: %d", space+1);
printf("\nTotal Number of Characters: %d", ct);
getch( );
return 0;
}
Explanation:-
Here the 1st condition (ch==10) checking whether the line is ended or not. And ascii value of enter key is 10. (Note:- After completing every line, press enter key. Then only it counts as Line).
The 2nd condition (isspace(ch) checking for spaces between the words. And here, isspace is a special function and it checks whether the character is space or not.
Note:- After completing all the lines, press Ctrl + Z to stop the loop.

10 comments:

  1. hey...wats dat EOF in the while condition???

    ReplyDelete
  2. EOF indicates 'End of the File'. The loop will terminate when the user press Ctrl+Z (^z) in the output screen, which is equal to 'EOF'

    ReplyDelete
  3. write a c-program to find number of lines, words and characters in a given text file using command line arguments
    please help me with this program!!!!!!

    ReplyDelete
  4. write a c program to find total no of lines,total no of characters,total no of words using text editor????
    please help me with this program....

    ReplyDelete
  5. write a c program about macroprocessor using SIC/XE machine????

    ReplyDelete
  6. Thanks my dear,your code is free from bugs thanks a lot......

    ReplyDelete
  7. how to sort a character array without using strcmp( )??

    ReplyDelete
  8. There is one problem with this program if we press ctrl+z even before typing anything on output screen it shows no of lines=1,no of word=1 and no of charecters=0 when we enter there nothing it has to show us the output zero please correct this error

    ReplyDelete
  9. IS there an alternate method

    ReplyDelete