问题
Can anyone help me in printing the string
aligned to center in C?
For example:
int main()
{
int a = 20;
char x[10] = "Hello";
char y[10] = "Hello";
printf ("%*s\n",a, x );
printf ("%-*s\n",a,y);
}
In the above the first prints Hello
aligned to the right and the second printf
to the left like this
Hello
Hello
restricting the length for each to 20.
Is there a way to print Hello aligned to the center.
Hello
restricting the total length to 20.
Thankyou in advance
回答1:
In general, you probably want to avoid centre-aligned text, especially on a console, but in any case; this will take some doing.
The (likely) reason that printf doesn't know how to do this to begin with is that it's not an entirely deterministic operation; not all strings can be centred in all spaces.
Notably, unless space_for_string - strlen(string)
is an even number, you're going to have to make a choice regarding where you want the string to be shifted to.
In any case, you do want to use the "%*s"
specifier, but the number you feed into the width field can't be as simple as a flat 20
.
Instead you want to feed it half the width of your space plus half the width of your string.
int main()
{
int space = 20;
char *string = "Hello";
printf ("%*s\n", space / 2 + strlen(string) / 2,string);
}
回答2:
There is no standardised way to do this with the C stdio
library. You would have to write your own code to do it with *printf
calls or with your own printf
format specifier.
This is a fast and dirty way to do it:
int main()
{
int a = 20;
char x[] = "Hello";
int length = strlen(x);
printf ("%*s%*c"
,((a - length) >> 1) + length // string length + padding spaces
, x
, ((a - length) >> 1) + ((a - length) & 1) // tailing spaces
, ' '
);
}
回答3:
You can use ncurses library to get to center of a line in terminal and printing your text if you are working on linux.
回答4:
You could, as Williham Totland's answer shows just calculate the required spaces, and use printf
. Since I'm in the process of refreshing my knowledge (limited though it is), on memory management, I came up with this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int a = 20;
char x[10] = "Hello";
char *spaces_ptr;//padding str pointer
a = (a-strlen(x))/2;
//allocate the required mem, calloc ensures zero terminated
spaces_ptr = calloc(a + 1, sizeof(char));
memset(spaces_ptr, ' ', a);//set a chars to hold spaces
//print
printf("%s%s", spaces, x);
free(spaces);//free memory
return 0;
}
Using sprintf
or strncat
is an option, too, of course... or even:
char *fullStr = calloc(strlen(x) + a + 1, sizeof(char));
memset(fullStr, ' ', a);
strncpy(fullStr+a, x, strlen(x));
puts(fullStr);
free(fullStr);
Note that this last snippet is an afterthought, as is the entire answer, really... anyway check this codepad to see it in action
回答5:
Take a look at my tiny library: libTprint, the code is very simple and you should be able to understand how does it align texts.
Hope it helps you !
来源:https://stackoverflow.com/questions/19492858/how-do-i-print-center-aligned-string-in-c