Tuesday, July 10, 2018

Program 6


TEXT FILE HANDLING - II


Write a program to print each word of a text file in reverse.

//Program to print each word of a text file in reverse

#include<fstream.h>
#include<string.h>
#include<stdio.h>
#include<conio.h>
#include<process.h>

void main ( )
 {
  clrscr ( );
   ofstream fout("poem.txt");
   char str[500];
  cout<<”Enter  text .Press * to stop :”;
  cin.getline(str,500,’*’ );
  fout<<str;
 fout.close();
  cout<<”\n File Created..”;
 cout<<”\n Reversing Each word in the file\n”;
  ifstream fin ( "poem.txt" ) ;
  if ( ! fin )
   {
    cout<< "Error opening file";
    getch ( ) ;
    exit ( 0 ) ;
   }
  
   char str [ 100 ] ;
   while( ! fin . eof ( ) )
    {
     fin . getline ( str, 100 , '  ' ) ;  // read a word

     int k = strlen ( str ) ;           // find length of the word

     for ( int i = k-1 ;  i>=0 ;  --i )        
       cout  <<  str [ i ] ;              // print from the end of the word
     cout << "  " ;

    }
   getch ( ) ;
   fin.close ( )  ;
  }


No comments:

Post a Comment