
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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; }
- Related Articles
- Displaying Source code of RFC in SAP system
- Read the Source Code of Shell Commands on Linux
- Accessing HTML source code using Python Selenium.
- Source code of ABAP reports without restriction
- How to retrieve source code from Python objects?
- How to Find out the source code of a transaction in SAP?
- How to Fetch source code of the transaction note in SAP R/3?
- How to represent the source code of an object with JavaScript Arrays?
- Accessing Source code of SAP Transport without using SAP system
- Atom – A Hackable Text and Source Code Editor for Linux
- How can we load a source code into JShell in Java 9?
- Decrypting source message from a code based on some algorithm in JavaScript
- Usage of subqueries in internal table as condition in SAP ABAP source code.
- How can we see the source code of a particular MySQL stored procedure?
- How can we see the source code of a particular MySQL stored function?
