Question: To count open blocks, we need to count how many curly braces get opened and closed on each line, so we need to count

To count open blocks, we need to count how many curly braces get opened and closed on each line so we need to countand Write a function int countChar (string line, char c); that scans the 1line and returns the number of occurrences of the character c Write a new program indent.cpp that enhances the program from the previous task. As it reads the input line by line, it should also count the number of open and closed in it and keep track of how many blocks is currently open at the beginning of each line In the listing below, the number of open blocks is shown on the left 0 int main()( // Ні, Im a program! 1 int x# 1; for(int i = 0; ǐ く 10; i++) { 2 cout<<i; 2 cout << endl; Then instead of printing the number of open blocks, add that number of tabs t at the beginning of each line, and you will get: int main() 7Hi, Im a program for(int i 0; i < 10; i++) { cout << i; cout << endl; II-closing for Loop II--closing main Notice that the closing curly braces are indented one level further than what they should be , its To fix that, when indenting the line, check its very first character. If it is a closing curly brace indentation level should be reduced by one int main() 7Hi, Im a program int x 1; for(int i = 0; ǐ < 10; i++) { cout << i; cout << endl;

To count open blocks, we need to count how many curly braces 

To count open blocks, we need to count how many curly braces get opened and closed on each line, so we need to count { and } Write a function int countChar(string line, char c); that scans the line and returns the number of occurrences of the character c. Write a new program indent.cpp that enhances the program from the previous task. As it reads the input line by line, it should also count the number of open and closed { } in it, and keep track of how many blocks is currently open at the beginning of each line. In the listing below, the number of open blocks is shown on the left: int main(){ 1 // Hi, I'm a program! int x - 1; 1 for (int i = e; i < 10; i+) { cout < < i; 2 cout < < endl3; } 1 Then instead of printing the number of open blocks, add that number of tabs '\t' at the beginning of each line, and you will get: int main(){ // Hi, I'm a program! int x = 1; for (int i = 0; i < 10; i++) { cout < < i; cout < < endl; } // Shortcomings of our program: No support for loops and if statements that don't have curly braces. For example, if (c -- 'A') S = S + C; will be incorrectly indented as if (c 'A') == S = s + C; No support for // and * */. Commented out curly braces should not affect indentation Symbols {, } inside string and char literals are misinterpreted as blocks if (true) { s = "{{"; t = "ABC"; } will be incorrectly indented as if (true) { s = "{{"; t = "ABC"; }

Step by Step Solution

3.49 Rating (156 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Check the code below include ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Document Format (2 attachments)

PDF file Icon

635e0a5119139_180943.pdf

180 KBs PDF File

Word file Icon

635e0a5119139_180943.docx

120 KBs Word File

Students Have Also Explored These Related Programming Questions!