How to center-align text in a terminal?
I want my text to be horizontally centered in the terminal. How can I do this in C?
+3
FredN
source
to share
3 answers
To expand on @eyalm's answer: if you got COLUMNS
var, you can center the lines like this:
int columns = strtol(getenv("COLUMNS"), NULL, 10);
int fwidth = strlen(s) + (columns - strlen(s)) / 2;
printf("%*s\n", fwidth, s);
+3
user529758
source
to share
If you are working with bash use an environment variable COLUMNS
to get the width and calculate the center.
+2
eyalm
source
to share
If you are lazy, how can I write all this code, this is a simple solution.
Console.WriteLine(" Hello World");
Console.ReadLine();
Add more space if needed to the center lol
+1
TheBoringGuy
source
to share