Re: Centering text on the 80 column display

From: Daniel Koepke (dkoepke@CALIFORNIA.COM)
Date: 03/29/98


On Sat, 28 Mar 1998, Judson Powers wrote:

->short   dist = 40 - (strlen(theString));
->short   count;
->char*   outspc = malloc(dist);
->for (count = 0; count < dist; count++)
->        outspc[count] = ' ';
->printf("%s", outspc);

That's not really centered, it's off because you only subtract the
length of the string from the center.  This means that if you were
centering a 4 character long string ("this") in 20 spaces, you'd get
("dist = 10-4 = 6"), "      this          ".  You probably want,

  dist = (int) ((width/2) - (strlen(theString) / 2));

which, again using 20 instead of 80 (and hence, 10 instead of 40 after
dividing by two in the formula), you'd get, "        this        ".

The number of spaces for the right side should be: "width - strlen -
padding".  This handles cases of lopsided spacing (i.e., when there
has to be more spaces on the right side than the left [or vice
versa]).

-dak


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/15/00 PST