How do I print center aligned string in C

Kranthi Kumar

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

Williham Totland

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);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I print output so that the variables are column aligned?

From Dev

How do I print 4 numbers as a string in C?

From Dev

How do I print "\n" explicitly in a string in c++

From Dev

How do I print "\n" explicitly in a string in c++

From Dev

How do I print this in C?

From Dev

How do I put a button A under button B such that the left side of A is vertically aligned with the center of B?

From Dev

How Do I check a Memory address is 32 bit aligned in C

From Dev

How do I print file permissions as a string?

From Dev

How do I get a variable to print a string

From Dev

How do I print the matched string?

From Dev

How do I get a variable to print a string

From Dev

c#, How can i print in Datagridview each row center?

From Dev

How do I easily and neatly print multiple objects using a toString method, that are also aligned?

From Dev

C++ unable to store aligned string in a string variable and print it

From Dev

How do I print pdf files in c?

From Dev

How do I print this pattern in C?

From Dev

How do I print a strucure in c++?

From Dev

How can I center a right-aligned DIV?

From Dev

How can i create a navbar with center aligned links using Materialize?

From Dev

How can i make the horizontal navigation menu center aligned

From Dev

c++ How do I print the value of a String attribute of a variable of another type?

From Dev

print 2 dimentional array center aligned

From Dev

How do I center the numbers for this C triangular number generator?

From Dev

How do I print a String diagonally from right to left?

From Dev

How do I print the line number of each line in a string?

From Dev

How do I print a vector of u8 as a string?

From Dev

How do I print numeric values with a string in R

From Dev

How do I print a '%' sign using string formatting?

From Dev

How do I print out every string in a char** to printf()?

Related Related

  1. 1

    How do I print output so that the variables are column aligned?

  2. 2

    How do I print 4 numbers as a string in C?

  3. 3

    How do I print "\n" explicitly in a string in c++

  4. 4

    How do I print "\n" explicitly in a string in c++

  5. 5

    How do I print this in C?

  6. 6

    How do I put a button A under button B such that the left side of A is vertically aligned with the center of B?

  7. 7

    How Do I check a Memory address is 32 bit aligned in C

  8. 8

    How do I print file permissions as a string?

  9. 9

    How do I get a variable to print a string

  10. 10

    How do I print the matched string?

  11. 11

    How do I get a variable to print a string

  12. 12

    c#, How can i print in Datagridview each row center?

  13. 13

    How do I easily and neatly print multiple objects using a toString method, that are also aligned?

  14. 14

    C++ unable to store aligned string in a string variable and print it

  15. 15

    How do I print pdf files in c?

  16. 16

    How do I print this pattern in C?

  17. 17

    How do I print a strucure in c++?

  18. 18

    How can I center a right-aligned DIV?

  19. 19

    How can i create a navbar with center aligned links using Materialize?

  20. 20

    How can i make the horizontal navigation menu center aligned

  21. 21

    c++ How do I print the value of a String attribute of a variable of another type?

  22. 22

    print 2 dimentional array center aligned

  23. 23

    How do I center the numbers for this C triangular number generator?

  24. 24

    How do I print a String diagonally from right to left?

  25. 25

    How do I print the line number of each line in a string?

  26. 26

    How do I print a vector of u8 as a string?

  27. 27

    How do I print numeric values with a string in R

  28. 28

    How do I print a '%' sign using string formatting?

  29. 29

    How do I print out every string in a char** to printf()?

HotTag

Archive