From 7e0f021a9aec35fd8e6725e87e3313b101d26f5e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 27 Jan 2008 11:37:44 +0100 Subject: Initial import (2.0.2-6) --- reference/C/CONTRIB/SNIP/center.c | 49 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100755 reference/C/CONTRIB/SNIP/center.c (limited to 'reference/C/CONTRIB/SNIP/center.c') diff --git a/reference/C/CONTRIB/SNIP/center.c b/reference/C/CONTRIB/SNIP/center.c new file mode 100755 index 0000000..f078a52 --- /dev/null +++ b/reference/C/CONTRIB/SNIP/center.c @@ -0,0 +1,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 +#include +#include +#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 -- cgit v1.2.3-54-g00ecf