Rearrange the given source code

Given a string str which contains the source code (C/C++/Java code) which is not arranged in a proper way, the task is to format that code in a proper way and print the result.

Examples:

Input: str = "#include<stdio.h>int main(){ cout << \"geeksforgeeks\" }"
Output:
#include<stdio.h>
int main()
{
 cout << "geeksforgeeks" 
}

Input: str = "#include<stdio.h>int main() { int a, b, c; c=a+b; printf(\"%d\", c); return 0; }"
Output:
#include<stdio.h>
int main() 
{
 int a, b, c;
 c=a+b;
 printf("%d", c);
 return 0;

}

Approach: Check various conditions like #, {, }, (, ), <, >, ; and format the code as required. If no changes are required or the code cannot be formatted then print the code as it is.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to rearrange the given source code
#include <bits/stdc++.h>
using namespace std;
  
// Function to rearrange the code
void rearrange(string str)
{
    string replaced;
  
    int Open_paren = 0, Open_Braces = 0;
    int Lessrthan = 0, i = 0, j = 0;
  
    do {
  
        // Various Conditions For The Code
        if (str[i] == '#' || str[i] == '<' || str[i] == '>'
            || str[i] == ';' || str[i] == '}' || str[i] == '{'
            || str[i] == '(' || str[i] == ')') {
  
            // Check for opening braces
            if (str[i] == '{')
                Open_Braces++;
  
            // Check for closing braces
            if (str[i] == '}')
                Open_Braces--;
  
            // For less than symbol
            if (str[i] == '<' && Open_paren == 0)
                Lessrthan++;
  
            // For greater than symbol
            if (str[i] == '>' && Open_paren == 0)
                Lessrthan--;
  
            // For open paranthesis
            if (str[i] == '(') {
                Lessrthan = 0;
                Open_paren++;
            }
  
            // For closing paranthesis
            if (str[i] == ')')
                Open_paren--;
  
            if (Open_paren > 0) {
                replaced += str[i];
            }
            else {
  
                // Replace the plain code
                // for closing parenthesis
                if (str[i] == ')')
                    replaced += str[i];
  
                // Replace the code for open and close braces
                else if (str[i] == '{' || str[i] == '}') {
                    replaced += '\n';
                    replaced += str[i];
                    replaced += '\n';
                }
                else if (Lessrthan > 0)
                    replaced += str[i];
  
                // Replace the code for # symbol
                else if (str[i] == '#') {
                    replaced += '\n';
                    replaced += str[i];
                }
                else {
                    replaced += str[i];
                    replaced += '\n';
                }
            }
        }
  
        // If all conditions do not
        // work then print the code as it is
        else {
            replaced += str[i];
        }
  
        i++;
  
    } while (i < str.length());
    replaced += '\0';
  
    // Print formatted code
    for (i = 0; i < replaced.length(); i++)
        cout << replaced[i];
    printf("\n");
}
  
// Driver Code
int main()
{
  
    string str = "#include<stdio.h>int main()"
                 "{ int a, b, c; c=a+b; printf(\"%d\", c);"
                 " return 0; }";
  
    // Function call
    rearrange(str);
  
    return 0;
}

chevron_right


Output:

#include<stdio.h>
int main()
{
 int a, b, c;
 c=a+b;
 printf("%d", c);
 return 0;
 
}


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.