home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 187_01 / c_just.c < prev    next >
C/C++ Source or Header  |  1985-12-30  |  2KB  |  49 lines

  1. /*@*****************************************************/
  2. /*@                                                    */
  3. /*@ c_just - center a string in a buffer to a given    */
  4. /*@        length.  See r_just and l_just also.        */
  5. /*@                                                    */
  6. /*@   Usage:     c_just(string, size);                 */
  7. /*@       returns the address of the modified string.  */
  8. /*@       NOTE:  There must be size+1 character        */
  9. /*@              spaces avail in string.               */
  10. /*@                                                    */
  11. /*@*****************************************************/
  12.  
  13. /***********************************************************************/
  14. /*                                                                     */
  15. /*    Center string str in size length field.                            */
  16. /*                                                                     */
  17. /***********************************************************************/
  18.  
  19. char *c_just(str, size)
  20. char *str;
  21. int size;
  22. {
  23.     char *s, *d;
  24.     int len, count;
  25.  
  26.     len = strlen(trim(str));        /* get string length */
  27.  
  28.     if (len > size)                    /* truncate, if necessary */
  29.         str[size] = 0x00;
  30.     else if (len < size) {
  31.         d = str + len + (size - len) / 2;        /* copy to leave room */
  32.         s = str + len;
  33.         count = len + 1;
  34.         while (count--)
  35.             *d-- = *s--;
  36.         count = (size - len) / 2;    /* number of blanks to insert */
  37.         s = str;
  38.         while (count--)
  39.             *s++ = ' ';                /* add leading blanks */
  40.         count = (size - len) - (size - len) / 2;    /* number of blanks to insert */
  41.         s = str + strlen(str);
  42.         while (count--)
  43.             *s++ = ' ';                /* add trailing blanks */
  44.         *s = 0x00;                    /* and restore end of string */
  45.     }
  46.  
  47.     return str;
  48. }
  49.