I have been trying to make a function in C++ for a gameserver DLL which aligns given text to center before returning the new string to Lua for processing. I have spent quite
Here's another way that will ensure the text is centered within a given width and is padded left and right with spaces.
std::string center(const std::string s, const int w) {
std::stringstream ss, spaces;
int pad = w - s.size(); // count excess room to pad
for(int i=0; i0 && pad%2!=0) // if pad odd #, add 1 more space
ss << " ";
return ss.str();
}
This could be written more elgantly or concisely.