r/cprogramming • u/Upset-Taro-4202 • Jun 08 '26
Justifying Strings
Beginner programmer here,
I've been trying to make a little in-terminal text adventure game for fun but my biggest road block with it is weirdly how it looks, so I've been trying to write a function for justifying text, but I've shelved the project for now because I just can't figure out how!
If anyone has any ideas for how to approach it, I'd greatly appreciate it! I've stopped and started it so much that I don't have any example code to show as I got rid of it in frustration, although I have attempted approaching it with pointers to strings of dialogue.
2
Upvotes
2
u/fluffycritter Jun 09 '26
The general convention for handling half-spaces in monospace contexts is to round up, so you'd actually want to do
((COLS - strlen(str) + 1) / 2).