C++ Programming Blog

 
 
 

Image

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

 int main( )
    {
       clrscr( );

       fstream file(\"CP-11.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 Input[85]={NULL};
       char *Output;
       char Assignments[2][100][55]={NULL};

       int number_of_replacements=0;

       do
      {
         strset(Input,NULL);

         file.getline(Input,80);

         number_of_replacements=atoi(Input);

         if(number_of_replacements==0)
        break;

         for(int i=0;i<100;i++)
        {
           strset(Assignments[0][i],NULL);
           strset(Assignments[1][i],NULL);
        }

         for(int j=0;j<number_of_replacements;j++)
        {
           strset(Input,NULL);

           file.getline(Input,80);

           int k=0;
           int l=1;

           Assignments[0][j][0]=\'{\';

           do
              {
             Assignments[0][j][l]=Input[k];
             k++;
             l++;
              }
           while(Input[k]!=\'=\');

           Assignments[0][j][l]=\'}\';
           k++;
           l=0;

           do
              {
             Assignments[1][j][l]=Input[k];
             k++;
             l++;
              }
           while(Input[k]!=NULL && Input[k]!=\' \');
        }

         strset(Input,NULL);

         file.getline(Input,80);

         int number_of_braces=0;

         for(int m=0;m<strlen(Input);m++)
        {
           if(Input[m]==\'{\')
              number_of_braces++;
        }

         Output=new char[(80+(55*number_of_braces))];

         strset(Output,NULL);
         strcpy(Output,Input);

         char *Ptr=NULL;

         for(int n=0;n<number_of_replacements;n++)
        {
           for(int o=0;o<number_of_replacements;o++)
              {
             Ptr=strstr(Output,Assignments[0][o]);

             if(Ptr!=NULL)
                {
                   int replace_point=(strlen(Output)-strlen(Ptr));

                   char *Temp=new char[strlen(Output)];

                   strset(Temp,NULL);
                   strcpy(Temp,Output);
                   strset(Output,NULL);

                   for(int p=0;p<replace_point;p++)
                  Output[p]=Temp[p];

                   strcat(Output,Assignments[1][o]);

                   p+=strlen(Assignments[1][o]);

                   int q=replace_point+strlen(Assignments[0][o]);

                   for(;q<strlen(Temp);p++,q++)
                 Output[p]=Temp[q];

                   delete Temp;
                }
              }
        }

         cout<<Output<<endl;

         delete Output;
      }
       while(1);

       file.close( );

       getch( );
       return 0;
    }

 
 
Didn't find what you were looking for? Find more on Program to do variable replacement in a line of text