Rearrange the given source code
Last Updated :
02 Apr, 2023
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++
#include <bits/stdc++.h>
using namespace std;
void rearrange(string str)
{
string replaced;
int Open_paren = 0, Open_Braces = 0;
int Lesserthan = 0, i = 0, j = 0;
do {
if (str[i] == '#' || str[i] == '<' || str[i] == '>'
|| str[i] == ';' || str[i] == '}' || str[i] == '{'
|| str[i] == '(' || str[i] == ')' ) {
if (str[i] == '{' )
Open_Braces++;
if (str[i] == '}' )
Open_Braces--;
if (str[i] == '<' && Open_paren == 0)
Lesserthan++;
if (str[i] == '>' && Open_paren == 0)
Lesserthan--;
if (str[i] == '(' ) {
Lesserthan = 0;
Open_paren++;
}
if (str[i] == ')' )
Open_paren--;
if (Open_paren > 0) {
replaced += str[i];
}
else {
if (str[i] == ')' )
replaced += str[i];
else if (str[i] == '{' || str[i] == '}' ) {
replaced += '\n' ;
replaced += str[i];
replaced += '\n' ;
}
else if (Lesserthan > 0)
replaced += str[i];
else if (str[i] == '#' ) {
replaced += '\n' ;
replaced += str[i];
}
else {
replaced += str[i];
replaced += '\n' ;
}
}
}
else {
replaced += str[i];
}
i++;
} while (i < str.length());
replaced += '\0' ;
for (i = 0; i < replaced.length(); i++)
cout << replaced[i];
printf ( "\n" );
}
int main()
{
string str = "#include<stdio.h>int main()"
"{ int a, b, c; c=a+b; printf(\"%d\", c);"
" return 0; }" ;
rearrange(str);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
public class Main {
public static void rearrange( char [] str)
{
String replaced = "" ;
int Open_paren = 0 , Open_Braces = 0 ;
int Lesserthan = 0 , i = 0 , j = 0 ;
do {
if (str[i] == '#' || str[i] == '<' || str[i] == '>'
|| str[i] == ';' || str[i] == '}' || str[i] == '{'
|| str[i] == '(' || str[i] == ')' ) {
if (str[i] == '{' )
Open_Braces++;
if (str[i] == '}' )
Open_Braces--;
if (str[i] == '<' && Open_paren == 0 )
Lesserthan++;
if (str[i] == '>' && Open_paren == 0 )
Lesserthan--;
if (str[i] == '(' ) {
Lesserthan = 0 ;
Open_paren++;
}
if (str[i] == ')' )
Open_paren--;
if (Open_paren > 0 ) {
replaced += str[i];
}
else {
if (str[i] == ')' )
replaced += str[i];
else if (str[i] == '{' || str[i] == '}' ) {
replaced += '\n' ;
replaced += str[i];
replaced += '\n' ;
}
else if (Lesserthan > 0 )
replaced += str[i];
else if (str[i] == '#' ) {
replaced += '\n' ;
replaced += str[i];
}
else {
replaced += str[i];
replaced += '\n' ;
}
}
}
else {
replaced += str[i];
}
i++;
} while (i < str.length);
replaced += '\0' ;
for (i = 0 ; i < replaced.length(); i++)
System.out.print(replaced.charAt(i));
System.out.println( "\n" );
}
public static void main(String[] args) {
String str = "#include<stdio.h>int main() { int a, b, c; c=a+b; printf(\"%d\", c); return 0; }" ;
rearrange(str.toCharArray());
}
}
|
Python3
def rearrange( str ):
replaced = ""
Open_paren = 0
Open_Braces = 0
Lesserthan = 0
i = 0
j = 0
while i < len ( str ):
if str [i] = = '#' or str [i] = = '<' or str [i] = = '>' or str [i] = = ';' or str [i] = = '}' or str [i] = = '{' or str [i] = = '(' or str [i] = = ')' :
if str [i] = = '{' :
Open_Braces + = 1
if str [i] = = '}' :
Open_Braces - = 1
if str [i] = = '<' and Open_paren = = 0 :
Lesserthan + = 1
if str [i] = = '>' and Open_paren = = 0 :
Lesserthan - = 1
if str [i] = = '(' :
Lesserthan = 0
Open_paren + = 1
if str [i] = = ')' :
Open_paren - = 1
if Open_paren > 0 :
replaced + = str [i]
else :
if str [i] = = ')' :
replaced + = str [i]
elif str [i] = = '{' or str [i] = = '}' :
replaced + = '\n'
replaced + = str [i]
replaced + = '\n'
elif Lesserthan > 0 :
replaced + = str [i]
elif str [i] = = '#' :
replaced + = '\n'
replaced + = str [i]
else :
replaced + = str [i]
replaced + = '\n'
else :
replaced + = str [i]
i + = 1
replaced + = '\0'
for i in range ( len (replaced)):
print (replaced[i], end = '')
print ( "\n" )
str = "#include<stdio.h>int main() { int a, b, c; c=a+b; printf(\"%d\", c); return 0; }"
rearrange( list ( str ))
|
C#
using System;
public class MainClass {
public static void rearrange( char [] str) {
string replaced = "" ;
int Open_paren = 0, Open_Braces = 0;
int Lesserthan = 0, i = 0, j = 0;
do {
if (str[i] == '#' || str[i] == '<' || str[i] == '>'
|| str[i] == ';' || str[i] == '}' || str[i] == '{'
|| str[i] == '(' || str[i] == ')' ) {
if (str[i] == '{' )
Open_Braces++;
if (str[i] == '}' )
Open_Braces--;
if (str[i] == '<' && Open_paren == 0)
Lesserthan++;
if (str[i] == '>' && Open_paren == 0)
Lesserthan--;
if (str[i] == '(' ) {
Lesserthan = 0;
Open_paren++;
}
if (str[i] == ')' )
Open_paren--;
if (Open_paren > 0) {
replaced += str[i];
}
else {
if (str[i] == ')' )
replaced += str[i];
else if (str[i] == '{' || str[i] == '}' ) {
replaced += '\n' ;
replaced += str[i];
replaced += '\n' ;
}
else if (Lesserthan > 0)
replaced += str[i];
else if (str[i] == '#' ) {
replaced += '\n' ;
replaced += str[i];
}
else {
replaced += str[i];
replaced += '\n' ;
}
}
}
else {
replaced += str[i];
}
i++;
} while (i < str.Length);
replaced += '\0' ;
for (i = 0; i < replaced.Length; i++)
Console.Write(replaced[i]);
Console.WriteLine( "\n" );
}
public static void Main( string [] args) {
string str = "#include<stdio.h>int main() { int a, b, c; c=a+b; printf(\"%d\", c); return 0; }" ;
rearrange(str.ToCharArray());
}
}
|
Javascript
function rearrange(str) {
let replaced = "" ;
let Open_paren = 0;
let Open_Braces = 0;
let Lesserthan = 0;
let i = 0;
let j = 0;
while (i < str.length) {
if (str[i] === '#' || str[i] === '<' || str[i] === '>' || str[i] === ';' || str[i] === '}' || str[i] === '{' || str[i] === '(' || str[i] === ')' ) {
if (str[i] === '{' ) {
Open_Braces += 1;
}
if (str[i] === '}' ) {
Open_Braces -= 1;
}
if (str[i] === '<' && Open_paren === 0) {
Lesserthan += 1;
}
if (str[i] === '>' && Open_paren === 0) {
Lesserthan -= 1;
}
if (str[i] === '(' ) {
Lesserthan = 0;
Open_paren += 1;
}
if (str[i] === ')' ) {
Open_paren -= 1;
}
if (Open_paren > 0) {
replaced += str[i];
} else {
if (str[i] === ')' ) {
replaced += str[i];
}
else if (str[i] === '{' || str[i] === '}' ) {
replaced += '\n' ;
replaced += str[i];
replaced += '\n' ;
} else if (Lesserthan > 0) {
replaced += str[i];
}
else if (str[i] === '#' ) {
replaced += '\n' ;
replaced += str[i];
} else {
replaced += str[i];
replaced += '\n' ;
}
}
} else {
replaced += str[i];
}
i += 1;
}
replaced += '\0' ;
for (let i = 0; i < replaced.length; i++) {
process.stdout.write(replaced[i]);
}
console.log();
}
let str = "#include<stdio.h>int main() { int a, b, c; c=a+b; printf(\"%d\", c); return 0; }" ;
rearrange(str.split( "" ));
|
Output:
#include<stdio.h>
int main()
{
int a, b, c;
c=a+b;
printf("%d", c);
return 0;
}
Share your thoughts in the comments
Please Login to comment...