Tuesday, 9 June 2015

Program to read all words from a file and remove all words which are palindromes


Code for Program to read all words from a file and remove all words which are palindromes in C++ Programming



 



 



 
 # include <iostream.h>
 # include <fstream.h>
 # include <string.h>
 # include <stdlib.h>
 # include <conio.h>

 int main( )
    {
       clrscr( );

       fstream file("CP-03.txt",ios::in|ios::nocreate);

       if(!file)
      {
         cout<<"\n Unable to open the input file."<<endl;
         cout<<"\n Press any key to exit.";

         getch( );
         exit(EXIT_FAILURE);
      }

       char Text_line[90]={NULL};

       file.getline(Text_line,80);

       int number_of_text_lines=atoi(Text_line);

       for(int counter=0;counter<number_of_text_lines;counter++)
      {
         strset(Text_line,NULL);

         file.getline(Text_line,80);

         int i=0;
         int j=0;

         char Word[80]={NULL};
         char Reverse_word[80]={NULL};

         do
        {
           strset(Word,NULL);
           strset(Reverse_word,NULL);

           j=0;

           do
              {
             Word[j]=Text_line[i];
             Reverse_word[j]=Text_line[i];

             i++;
             j++;
              }
           while(Text_line[i]!=' ' && Text_line[i]!=NULL);

           strrev(Reverse_word);

           if(strcmp(Word,Reverse_word)!=0)
              cout<<Word;

           if(Text_line[i]!=NULL)
              cout<<" ";

           i++;
        }
         while(Text_line[i]!=NULL);

         cout<<endl;
      }

       file.close( );

       getch( );
       return 0;
    }

No comments:

Post a Comment