/* * @topic T00351 May 2 -- white space indentation * @brief Program detects { } braces to print indented text */ #include <iostream> #include <string> using namespace std; void indent( int level ) { cout << '\n'; while ( level > 0 ) { cout << ' '; --level; } } int main() { string input = "if cond { hello" " { zzzz }" " } " "else { bye }\n" ; int level = 0; for ( unsigned int idx = 0; idx < input.length(); ++idx ) { if ( input[ idx ] == '{' ) { //increase indentation ++level; indent( level ); } if ( input[ idx ] == '}' ) { //reduce indentation indent( level ); --level; } cout << input[ idx ]; } return 0; }