EDIT: This is A duplicate.
Set variable text column width in printf & The simplest way of printing a portion of a char[] in C
Answered my question
I want to output a menu in C but I can't get it look good. I have search functions to do this in C and cannot find any?
A website used C# and String.Format()
to achieve this, I wonder if this is possible to do in C ?
> [C#] Console.WriteLine("-------------------------------");
> Console.WriteLine("First Name | Last Name | Age");
> Console.WriteLine("-------------------------------");
> Console.WriteLine(String.Format("{0,-10} | {1,-10} | {2,5}", "Bill",
> "Gates", 51)); Console.WriteLine(String.Format("{0,-10} | {1,-10} |
> {2,5}", "Edna", "Parker", 114));
> Console.WriteLine(String.Format("{0,-10} | {1,-10} | {2,5}", "Johnny",
> "Depp", 44)); Console.WriteLine("-------------------------------");
OUTput be like:
-------------------------------
First Name | Last Name | Age
-------------------------------
Bill | Gates | 51
Edna | Parker | 114
Johnny | Depp | 44
-------------------------------
In my own C I am trying to format one string and space or special character before and after such as:
--------------------------------
------This x costs y dollar-----
------This i costs j dollar-----
...
...
-------------------------------
The front would be %s the item name, and latter would be %d the cost.
My idea was like to print this in a loop, it loops m
(total items) times define a line
width of 30 total. Each loop find the length of the item name, use another loop to /10 of the cost to find it's len and add both of them together.
print ((line
-totalLen
)/2) in front, print the string and do the same after the string?
The small problem would be if the whole string is odd or even number.