Question
In this lab, we will develop a small utility program that can fix indentation in C or C++ source code files. It will have some
In this lab, we will develop a small utility program that can fix indentation in C or C++ source code files. It will have some limitations, but it will be able to cover a significant subset of valid C++ programs. Specifically, given a file with messed up indentation style:
int main(){ // Hi, I'm a program! int x = 1; for(int i = 0; i < 10; i++) { cout << i; cout << endl; } }
It will output a well-formatted program:
int main(){ // Hi, I'm a program! int x = 1; for(int i = 0; i < 10; i++) { cout << i; cout << endl; } }
Task A. Removing indentation
Before we make a program that indents source code files, lets make a program that unindents them.
Start by writing a function
string removeLeadingSpaces(string line);
that takes one line of code as input and returns its copy without leading spaces and tabs:
removeLeadingSpaces(" int x = 1; ") == "int x = 1; "
Make use of the function isspace defined in
Write a program unindent.cpp that reads input from cin and prints out each input line with leading spaces removed.
Example:
To test, create a badly indented file, for instance, called bad-code.cpp:
int main(){ // Hi, I'm a program! int x = 1; for(int i = 0; i < 10; i++) { cout << i; cout << endl; } }
Since our unindent program reads its input from cin, the badly indented text can be fed into it using standard input redirection:
$ ./unindent < bad-code.cpp int main(){ // Hi, I'm a program! int x = 1; for(int i = 0; i < 10; i++) { cout << i; cout << endl; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started