Rearrange the given source code in C++


We are given a string type variable, let's say, str which will be used to store the source code then calculate the size of the string and pass it to the function. The task is to rearrange the given source code and then print the result.

Let us see various input output scenarios for this −

Input − string str =

"#include <bits/stdc++.h>
using namespace std;
int main()"
   "{ int sum, first, second; sum = first + second; printf(\"%d\", c);"
   " return 0;}"

Output 

#include <bits/stdc++.h>
using namespace std;
int main(){
   int sum, first, second;
   sum = first + second;
   printf("%d", c);
   return 0;
}

Input − string str =

"#include<bits/stdc++.h>
 using namespace std;
int main()"
   "{ printf(\"%d\", c);"
   " return 0;}"

Output 

#include<bits/stdc++.h>
using namespace std;
int main(){
   printf("%d", c);
   return 0;
}

Approach used in the below program is as follows

  • Input a variable of string type, let’s say, str and calculate the size of a string and store it in a length named variable.

  • Pass the data to the function Rearrangement(str, length).

  • Inside the function Rearrangement(arr, length)

    • Declare a string type variable, let's say, str_1 and integer type variables as Parenthesis to 0, Braces to 0, count to 0, i to 0 and j to 0.

    • Start do-WHILE. Inside, check IF str[i] is '#' OR str[i] is '<' OR str[i] is '>' OR str[i] is ';' OR str[i] is '}' OR str[i] is '{' OR str[i] is '(' OR str[i] is ')' then check IF str[i] is '{' then increment the braces by 1.

    • Check IF str[i] is '}' then decrement the Braces by 1.

    • Check IF str[i] is '<' AND Parenthesis is 0 then increment the count by 1.

    • Check IF str[i] is '>' AND Parenthesis is 0 then decrement the count by 1.

    • Check IF str[i] is '(' then set count to 0 and increment the Parenthesis by 1.

    • Check IF str[i] is ')' then decrement the Parenthesisby 1.

    • Check IF Parenthesis greater than 0 then set str_1 to str_1 + str[i]. ELSE, check IF str[i] is ')' then set str_1 to str_1 + str[i].

    • ELSE IF, str[i] is '{' OR str[i] is '}' then set str_1 to str_1 + '\n', str_1 to str_1 + str[i] and str_1 to str_1 + '\n'.

    • ELSE IF, count is greater than 0 then set str_1 to str_1 + str[i].

    • ELSE IF, str[i] is '#' then set str_1 to str_1 + '\n' and str_1 to str_1 + str[i].

    • ELSE, set str_1 to str_1 + str[i] and str_1 to str_1 + '\n'.

    • ELSE, set str_1 to str_1 + str[i] and increment the i by 1.

    • Set str_1 to str_1 + '\0'.

    • Start loop FOR from i to 0 till i less than the length of a str_1 string. Inside the loop, print str_1[i].

  • Print the result.

Example

#include <bits/stdc++.h>
using namespace std;
void Rearrangement(string str, int length){
   string str_1;
   int Parenthesis = 0;
   int Braces = 0;
   int count = 0;
   int i = 0;
   int j = 0;
   do{
      if(str[i] == '#' || str[i] == '<' || str[i] == '>' || str[i] == ';' || str[i] == '}' || str[i] == '{'   || str[i] == '(' || str[i] == ')'){
         if(str[i] == '{'){
            Braces++;
         }
         if(str[i] == '}'){
            Braces--;
         }
         if(str[i] == '<' && Parenthesis == 0){
            count++;
         }
         if(str[i] == '>' && Parenthesis == 0){
            count--;
         }
         if(str[i] == '('){
            count = 0;
            Parenthesis++;
         }
         if(str[i] == ')'){
            Parenthesis--;
         }
         if(Parenthesis > 0){
            str_1 = str_1 + str[i];
         }
         else{
            if(str[i] == ')'){
               str_1 = str_1 + str[i];
            }
            else if(str[i] == '{' || str[i] == '}'){
               str_1 = str_1 + '\n';
               str_1 = str_1 + str[i];
               str_1 = str_1 + '\n';
            }
            else if(count > 0){
               str_1 = str_1 + str[i];
            }
            else if(str[i] == '#'){
               str_1 = str_1 + '\n';
               str_1 = str_1 + str[i];
            }
            else{
               str_1 = str_1 + str[i];
               str_1 = str_1 + '\n';
            }
         }
      }
      else{
            str_1 = str_1 + str[i];
         }
         i++;
   }while (i < length);
    str_1 = str_1 + '\0';
    for(i = 0; i < str_1.length(); i++){
       cout<< str_1[i];
    }
}
int main(){
      string str = "#include <bits/stdc++.h>using namespace std;int main()"
      "{ int sum, first, second; sum = first + second; printf(\"%d\", c);"
      " return 0;}";
      int length = str.length();
      Rearrangement(str, length);
      return 0;
}

Output

If we run the above code it will generate the following Output

#include <bits/stdc++.h>
using namespace std;
int main()
{
   int sum, first, second;
   sum = first + second;
   printf("%d", c);
   return 0;
}

Updated on: 02-Nov-2021

247 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements