Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Object Oriented ProgrammingRSS Feeds

Program to do variable replacement in a line of text

Posted By: Easy Tutor     Category: C++ Programming     Views: 2364

Write a program to do variable replacement in a line of text. You will be given the values of all variables. Variables will be enclosed in curly braces in the line of text. You have to repeatedly replace the variables i.e. if after replacement the line again contains a variable name in braces, replace it. If the braces contain an invalid name, don’t change it. Braces will always match.

The input file will contain multiple test cases. Each test case will begin with an integer N (0<N<100). The following N lines will contain assignments of the form X=Y where X and Y will be strings of lowercase and uppercase alphabets with a maximum length of 50 characters. The next line of text will contain maximum 80 characters and is to be processed. The input will terminate with a test case for which N=0 which should not be processed.

For each test case replace all variables as described in the problem and output the changed line.

Code for Program to do variable replacement in a line of text in C++ Programming

 # 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=newchar[(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=newchar[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;
    }
  
Share: 


Didn't find what you were looking for? Find more on Program to do variable replacement in a line of text Or get search suggestion and latest updates.

Easy Tutor
Easy Tutor author of Program to do variable replacement in a line of text is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!