home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / CENTER.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  1KB  |  52 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /* public domain by Jerry Coffin.  Tested with MSC 7.0  */
  4. /* written primarily for clarity, not speed...          */
  5. /* requires w_wrap.c and w_wrap.h from snippets.        */
  6.  
  7.  
  8. #include <conio.h>
  9. #include <string.h>
  10. #include "w_wrap.h"
  11.  
  12. void center(FILE *file, char *string, size_t width)
  13. {
  14.       char *line,*end_line;
  15.       size_t spaces;
  16.       int last = 0;
  17.       size_t len;
  18.  
  19.       word_wrap(string,width);
  20.       line = string;
  21.       while (!last)
  22.       {
  23.             end_line = strchr(line,'\n');
  24.             if (NULL==end_line)
  25.             {
  26.                   last = 1;
  27.                   end_line = strchr(line,'\0');
  28.             }
  29.             len = end_line - line;
  30.             spaces = (width - len) / 2 ;
  31.             fprintf(file,"\n%*c%*.*s",spaces,' ',len,len,line);
  32.             line = end_line + 1;
  33.       }
  34. }
  35.  
  36. #ifdef TEST
  37.  
  38. int main(void)
  39. {
  40.       char *string = "This is a long string to see if it will be"
  41.             " printed out correctly but I'm not sure whether it will work"
  42.             " correctly or not.  I guess we'll see when we try it out.";
  43.  
  44.       printf("\nHere's the string centered in 50 columns.\n");
  45.       center(stdout,string,50);
  46.       printf("\n\nAnd here it's centered in 72 columns.\n");
  47.       center(stdout,string,72);
  48.       return 0;
  49. }
  50.  
  51. #endif /* TEST */
  52.