How printf()
works?
If you want to fully understand the working of printf()
function, then refer this: http://www.cplusplus.com/reference/cstdio/printf/
In simple words, "%*.s|%.*s\\%.*s/%.*s|\n"
says that
- Set field width dynamically and print required amount of
''
- Print
|
- Set field width dynamically and print required amount of
':'
- Print
\
- Set field width dynamically and print required amount of
':'
- Print
/
- Set field width dynamically and print required amount of
':'
- Print
|
- Print newline character
\n
to go to the next line in console.
Similarly, you can figure out what "%*.s|%.*s/%.*s\\%.*s|\n"
will print.
Code conversion
I assume that you're pretty familiar with C++. It's better if you use string
class as you can then easily use substr()
member function to extract the required dots
substring to print. You can understand how setw()
works from here. The converted code is given below.
#include <iostream> // Input.Output
#include <string> // string class
#include <iomanip> // setw() function
int main(){
const int n=3,z=6;
std::string _dots(2*n+1,':');
for(int r = 0; r < n+1; r++){
std::cout<<std::setw(z-n)<<""<<"|"<<_dots.substr(0,r)<<"\\"<<_dots.substr(0,2*(n-r))<<"/"<<_dots.substr(0,r)<<"|"<<std::endl;
}
for(int r = n; r >= 0; r--){
std::cout<<std::setw(z-n)<<""<<"|"<<_dots.substr(0,r)<<"/"<<_dots.substr(0,2*(n-r))<<"\\"<<_dots.substr(0,r)<<"|"<<std::endl;
}
return 0;
}
Output
|\::::::/|
|:\::::/:|
|::\::/::|
|:::\/:::|
|:::/\:::|
|::/::\::|
|:/::::\:|
|/::::::\|