Tuesday, July 10, 2018

Program 5


TEXT FILE HANDLING - I


Write a program to count the number of words, lines and alphabets in a text file.
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
#include<ctype.h>
void main ( )
 {
          clrscr();
          int choice=1;
 char ch ;
          int ctrWords=0, ctrLines=0, ctrAlpha=0;

          while(1)
        { 
            cout<<”\n Menu\n 1. Create File \n 2. Count the no of words,lines and
              alphabets\n 3.Exit\n “;
            cout<<” Enter your choice:”;
            cin>>choice;
            switch(choice)
            {
              case 1: 
               {
         
                    ofstream fout("temp.txt");
                   char str[500];
                   cout<<”Enter text .Press * to stop :”;
                   cin.getline(str,500,’*’ );
                   fout<<str;
                   fout.close();
                   break;
                   }
             case 2:
                  {
                             ifstream fin("temp.txt");
                             if(!fin)
                             {
                               cout<<"\nError Opening File";
                                getch();
                                exit(0);
                              }
         
while( ! fin . eof ( ) )
                     {
                      fin . get ( ch ) ;
         
                        if( ch == '\n')
                             ctrLines++;
         
                    if( ch == ' '|| ch == '.'||ch=='\n')
                             ctrWords++;
         
                    if( isalpha(ch) )
                             ctrAlpha++;
           }
           fin . close ( ) ;
         
 cout<<"\nNo of Words= \t"<<ctrWords;
           cout<<"\nNo of Lines= \t"<<ctrLines;
           cout<<"\nNo of Alphabets=\t"<<ctrAlpha;
          break;
       }
   case 3:
                exit(0);
}
}
 getch();

}

No comments:

Post a Comment