Here's a quick and easy way to draw and center a title in a dash and pipe box using
printf()
:void titleBox(char const *title) { static char const border[] = "+------------------------------------+\n"; static int const borderWidth = sizeof(border) - sizeof("\n"); // minus '\n' and '\0' int paddingWidth = borderWidth - strlen(title); int leftPaddingWidth = paddingWidth / 2; int rightPaddingWidth = paddingWidth - leftPaddingWidth; printf(border); printf("%-*c", leftPaddingWidth, '|'); printf(title); printf("%*c\n", rightPaddingWidth, '|'); printf(border); }And the output looks like:
+------------------------------------+ | My Stuff | +------------------------------------+
No comments:
Post a Comment