File: center.c

package info (click to toggle)
c-cpp-reference 2.0.2-8
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 8,016 kB
  • ctags: 4,612
  • sloc: ansic: 26,960; sh: 11,014; perl: 1,854; cpp: 1,324; asm: 1,239; python: 258; makefile: 119; java: 77; awk: 34; csh: 9
file content (49 lines) | stat: -rwxr-xr-x 1,334 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/* public domain by Jerry Coffin.  Tested with MSC 7.0  */
/* written primarily for clarity, not speed...          */
/* requires w_wrap.c and w_wrap.h from snippets.        */


#include <stdio.h>
#include <conio.h>
#include <string.h>
#include "w_wrap.h"

void center(FILE *file, char *string, size_t width)
{
      char *line,*end_line;
      size_t spaces;
      int last = 0;
      size_t len;

      word_wrap(string,width);
      line = string;
      while (!last)
      {
            end_line = strchr(line,'\n');
            if (NULL==end_line)
            {
                  last = 1;
                  end_line = strchr(line,'\0');
            }
            len = end_line - line;
            spaces = (width - len) / 2 ;
            fprintf(file,"\n%*c%*.*s",spaces,' ',len,len,line);
            line = end_line + 1;
      }
}

#ifdef TEST

int main(void)
{
      char *string = "This is a long string to see if it will be"
            " printed out correctly but I'm not sure whether it will work"
            " correctly or not.  I guess we'll see when we try it out.";

      printf("\nHere's the string centered in 50 columns.\n");
      center(stdout,string,50);
      printf("\n\nAnd here it's centered in 72 columns.\n");
      center(stdout,string,72);
}

#endif