Code Snippets

  

C Source Code


Welcome to Dream.In.Code
Getting Help is Easy!

Join 107,710 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,109 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!




Centered Printf

A printf() type of function used to center text on an 80 character width display.

Submitted By: NickDMax
Actions:
Rating:
Views: 63

Language: C

Last Modified: August 6, 2008
Instructions: You will need to include the following headers: stdio.h, stdarg.h, string.h.

Snippet


  1. #include <stdio.h>
  2. #include <stdarg.h>
  3. #include <string.h>
  4.  
  5. int centerPrintf(const char *format, ...);
  6.  
  7. int main() {
  8.         centerPrintf("--- %s ----", "PROGRAM TITLE");
  9.         getchar();
  10.         return 0;
  11. }
  12.  
  13. /**
  14. *  centerPrintf(const char *format, ...) -- used to center text on the screen.
  15. *  Compatible with standard printf() type statements, but to the center the
  16. *     text the length of the text must be less than 80 chars. To try to ensure
  17. *     this the format text is limited to 70 characters. However it is left up
  18. *     to the user to ensure that the formatted text will be less than 80 chars.
  19. *   
  20. */
  21. int centerPrintf(const char *format, ...)
  22. {
  23.     char buffer[81];
  24.     char myformat[81];
  25.     char *ptr = myformat;
  26.     int len; //used to tell how much of the buffer was used.
  27.     int spaces;
  28.     va_list args; //the argument list
  29.     va_start (args, format); //set up the argument list to be passed to vsprintf()
  30.     len = strlen(format);
  31.     if (len < 70) {
  32.             //This is how we can pass along our argument list using
  33.             // the vsprintf function.
  34.             len = vsprintf (buffer, format, args);
  35.             spaces = 40 - len/2;
  36.             len += spaces;
  37.             //Sort of a check
  38.             if (len < 80) {
  39.                while (spaces--) { *ptr++ = 0x20; }
  40.                ++spaces;
  41.                while (buffer[spaces]) { *ptr++ = buffer[spaces++]}
  42.                *ptr = 0x00;
  43.                puts(myformat);
  44.             } else {
  45.                  len = 0;
  46.             }
  47.     } else {
  48.         len = 0; //if there is no room then don't do it.
  49.     }
  50.     va_end (args);
  51.     return len; //number of characters that we needed
  52. }
  53.  

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month