img
Question:
Published on: 21 November, 2024

Write a C- Program To Count The Lines, Words, Characters In A Given Text.

Answer:
#include  <conio.h> 
#include <stdio.h>

main()
{
      char text[200]; 
      int i,l,ch,w,sp; 
      clrscr();

      i=0;
      printf("\n Enter lines of text and press ^Z"); 
      while((text[i]=getchar())!=EOF)
      {
           i++;
      }
      printf("\n The number of characters is %d",i);

      text[i]='\0';
      l=0;
      ch=w=sp=0; 
      for(i=0;text[i]!='\0';i++)
      {
           ch++;
           if(text[i]==32 && text[i+1] != ' ')
           {
                w++;
                sp++;
           }
           if(text[i] == '\n')
           {
                l++; w++;
           }
       }
       printf("\n Total size of the text : %d",ch); 
       printf("\n Number of Words : %d",w+1); 
       printf("\n Number of Lines : %d",l); 
       printf("\n Number of Spaces : %d",sp); 
       getch();
}

Random questions