Re: Centering text on the 80 column display

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


On Sun, 29 Mar 1998, Judson Powers wrote:

->Thank you.  Had a minor brain short there.  However, you don't have to
->print spaces to the right of the string that's being centered.  Also, it's
->usually better to concat the spaces and the string and then print them, to
->reduces your I/O calls.

I know you don't _have_ to print spaces to the right of the string,
but you may _want_ to.  Also, it's important to notice that strlen()
counts non-visible characters (e.g., "\r\n"), so don't trust it to be
completely accurate.  Here's a version of strlen() you might find
useful for these purposes,

  int my_strlen(char * str)
  {
    int count, i;
    if (!str || !*str) return 0;

    for (i = 0; i < strlen(str); i++) {
      if (isprint(*(str+i))) count++;
      else if (*(str+i) == '\b') count--;
    }

    return count;
  }

The only possible strange thing the code does is subtract from the
count for \b characters.  \b isn't counted as a printable character,
but it does affect the string, which is odd.  For instance, "This
m\bhas eighteen." would (without the "else if" clause) return 19 but
only print "This has eighteen."

As for I/O stuff, that wasn't my code, it was the person's that I was
replying to.  And if I recall, the person made it clear that it's
better to do stuff in memory rather than through I/O system calls (can
we say, "duh," class?).

-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