What's Here?
- Members: 107,710
- Replies: 409,595
- Topics: 62,216
- Snippets: 2,253
- Tutorials: 604
- Total Online: 1,109
- Members: 42
- Guests: 1,067
Who's Online?
|
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!
|
A printf() type of function used to center text on an 80 character width display.
|
Submitted By: NickDMax
|
|
|
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
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
int centerPrintf(const char *format, ...);
int main() {
centerPrintf("--- %s ----", "PROGRAM TITLE");
getchar();
return 0;
}
/**
* centerPrintf(const char *format, ...) -- used to center text on the screen.
* Compatible with standard printf() type statements, but to the center the
* text the length of the text must be less than 80 chars. To try to ensure
* this the format text is limited to 70 characters. However it is left up
* to the user to ensure that the formatted text will be less than 80 chars.
*
*/
int centerPrintf(const char *format, ...)
{
char buffer[81];
char myformat[81];
char *ptr = myformat;
int len; //used to tell how much of the buffer was used.
int spaces;
va_list args; //the argument list
va_start (args, format); //set up the argument list to be passed to vsprintf()
len = strlen(format);
if (len < 70) {
//This is how we can pass along our argument list using
// the vsprintf function.
len = vsprintf (buffer, format, args);
spaces = 40 - len/2;
len += spaces;
//Sort of a check
if (len < 80) {
while (spaces--) { *ptr++ = 0x20; }
++spaces;
while (buffer[spaces]) { *ptr++ = buffer[spaces++]; }
*ptr = 0x00;
puts(myformat);
} else {
len = 0;
}
} else {
len = 0; //if there is no room then don't do it.
}
va_end (args);
return len; //number of characters that we needed
}
Copy & Paste
|
|
|
Programming
Web Development
Reference Sheets
Bye Bye Ads
Free DIC T-Shirt
Related Sites
Monthly Drawing
Partners
Top Contributors
Top 10 Kudos This Month
|