Pages

Wednesday, 17 January 2018

Count Word Character new line

Q  : Write a Program to count lines, words and characters with a definition that a word is any sequence of characters that does not contain a blank, tab or new line.

Solution :


/*==============================
     Girfa Student Help
     Program : Count Word Character new line
     More Program :http://girfahelp.blogspot.in/p/1.html
================================*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
     FILE *pt;
     char ch;
     int character=0,line=1,word=1;
     clrscr();
     pt=fopen("data.txt","r");

     if(pt==NULL)
     {
          printf("\nFile opening error");
          exit(1);
     }
     else
     {
          while(1)
          {
              ch=fgetc(pt);
              if(ch==EOF)
                   break;
              if(ch=='\n')
                   line++;
               if(ch==32 || ch=='\n')
              {
                   word++;
              }

              character++;

          }
          printf("\n\tCharacter=%d\tWord=%d\tline=%d",character,word,line);
     }
     getch();

}

No comments:

Post a Comment