One word to the right extreme and another to the left extreme

Discussion in 'C Programming' started by karthikbalaguru, Mar 18, 2009.

  1. Hi,
    I have a query based on the format
    specifier in printf.

    Consider the below code -

    #include<stdio.h>
    int main(void)
    {
    char *s = "Hello World";
    printf("%15.11s\n",s);
    printf("%-15.11s\n",s);
    return 0;
    }

    I got the below output :-
    Hello World
    Hello World

    Is ther any specifier to print the Hello World
    such that Hello goes to the left extreme and
    World to the Right extreme with spaces inbetweeen
    them ?

    That is, i am expecting the below output using
    any of the format specifier in printf :-
    Hello World

    Thx in advans,
    Karthik Balaguru
    karthikbalaguru, Mar 18, 2009
    #1
    1. Advertising

  2. karthikbalaguru

    Ben Pfaff Guest

    karthikbalaguru <> writes:

    > Is ther any specifier to print the Hello World
    > such that Hello goes to the left extreme and
    > World to the Right extreme with spaces inbetweeen
    > them ?
    >
    > That is, i am expecting the below output using
    > any of the format specifier in printf :-
    > Hello World


    No, there is no printf specifier to do that.
    --
    "...what folly I commit, I dedicate to you."
    --William Shakespeare, _Troilus and Cressida_
    Ben Pfaff, Mar 18, 2009
    #2
    1. Advertising

  3. On Mar 19, 12:47 am, Ben Pfaff <> wrote:
    > karthikbalaguru <> writes:
    > > Is ther any specifier to print the Hello World
    > > such that Hello goes to the left extreme and
    > > World to the Right extreme with spaces inbetweeen
    > > them ?

    >
    > > That is, i am expecting the below output using
    > > any of the format specifier in printf :-
    > > Hello      World

    >
    > No, there is no printf specifier to do that.


    As there is lot of tricks with format specifiers,
    a trick for the above output can be added into C.
    I think, there should be some support as below in
    the future releases of C to do that -
    printf("%-15.-5.5s\n",s);
    where the -5 is for having the Hello on
    the extreme left and the 5 is for having
    the World on the extreme right.

    Karthik Balaguru
    karthikbalaguru, Mar 18, 2009
    #3
  4. karthikbalaguru

    Ben Pfaff Guest

    karthikbalaguru <> writes:

    > I think, there should be some support as below in
    > the future releases of C to do that -
    > printf("%-15.-5.5s\n",s);
    > where the -5 is for having the Hello on
    > the extreme left and the 5 is for having
    > the World on the extreme right.


    In the 20 years or so that I have been writing C code, I do not
    recall ever needing such a feature.

    Why don't you just write a function to do what you want?
    --
    char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
    ={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6},*p
    =b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
    2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
    Ben Pfaff, Mar 18, 2009
    #4
  5. karthikbalaguru wrote:

    > Is ther any specifier to print the Hello World
    > such that Hello goes to the left extreme and
    > World to the Right extreme with spaces inbetweeen
    > them ?


    #include <stdio.h>

    int main(void)
    {
    const int leftchars = 7, spacing = 3, rightchars = 7;
    printf("With explicit padding space:\n"
    "%-*s%*s%*s\n\n", leftchars, "Hello", spacing, "",
    rightchars, "World");

    printf("Without explicit padding space:\n"
    "%-*s%*s\n", leftchars, "Hello", rightchars, "World");
    return 0;
    }

    [Output]

    With explicit padding space:
    Hello World

    Without explicit padding space:
    Hello World
    Martin Ambuhl, Mar 18, 2009
    #5
  6. karthikbalaguru <> writes:
    > On Mar 19, 12:47 am, Ben Pfaff <> wrote:
    >> karthikbalaguru <> writes:
    >> > Is ther any specifier to print the Hello World
    >> > such that Hello goes to the left extreme and
    >> > World to the Right extreme with spaces inbetweeen
    >> > them ?

    >>
    >> > That is, i am expecting the below output using
    >> > any of the format specifier in printf :-
    >> > Hello      World

    >>
    >> No, there is no printf specifier to do that.

    >
    > As there is lot of tricks with format specifiers,
    > a trick for the above output can be added into C.
    > I think, there should be some support as below in
    > the future releases of C to do that -
    > printf("%-15.-5.5s\n",s);
    > where the -5 is for having the Hello on
    > the extreme left and the 5 is for having
    > the World on the extreme right.


    And what should this do if the string doesn't contain any spaces? Or
    if it contains more than two words? Or if it has leading and trailing
    spaces? What about white space other than space characters?

    A printf format string is, in effect, written in a very small language
    of its own. It's not a general-purpose language; it's designed for
    one specific narrow purpose. Don't expect that language to become so
    sophisticated that it will do your programming for you, when the C
    language itself is more than powerful enough to do what you want.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Mar 18, 2009
    #6
  7. karthikbalaguru

    CBFalconer Guest

    karthikbalaguru wrote:
    >

    .... snip ...
    >
    > That is, i am expecting the below output using
    > any of the format specifier in printf :-


    If you want to know about printf formats and you are using Windoze
    I suggest you mount 4dos, n869.txt, Buergs list, and the following
    two alias'. Then the command 'printf' will tell you all you want
    to know. Instantaneously. Without cluttering the C newsgroup.

    [1] c:\>alias printf
    cstd 7.19.6.1

    [1] c:\>alias cstd
    list c:\stds\n869.txt /f%1 %2&

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
    CBFalconer, Mar 18, 2009
    #7
  8. On Mar 19, 1:06 am, Eric Sosman <> wrote:
    > karthikbalaguru wrote:
    > > Hi,
    > > I have a query based on the format
    > > specifier in printf.

    >
    > > Consider the below code -

    >
    > > #include<stdio.h>
    > > int main(void)
    > > {
    > > char *s = "Hello World";
    > > printf("%15.11s\n",s);
    > > printf("%-15.11s\n",s);
    > > return 0;
    > > }

    >
    > > I got the below output :-
    > > Hello World
    > > Hello World

    >
    > > Is ther any specifier to print the Hello World
    > > such that Hello goes to the left extreme and
    > > World to the Right extreme with spaces inbetweeen
    > > them ?

    >
    > No. The string[*] printed by "%s" and its variants
    > is one single thing, not a sequence of separable things.
    >
    > [*] With a precision specifier -- the ".11" in your
    > example -- the thing printed need only be a sequence of
    > characters, not necessarily a string.
    >
    > > That is, i am expecting the below output using
    > > any of the format specifier in printf :-
    > > Hello World

    >
    > You'll need to make your own decision about where
    > to divide the string and how much space to use. For
    > example, if you want to put the first five characters
    > on the left and the rest on the right, using fifteen
    > positions in all, you could write
    >
    > printf ("%.5s%10s\n", s, s+5);
    >
    > More generally, if you want to put the first m on the
    > left and the rest on the right, using n positions in
    > all (m and n both ints),
    >
    > printf ("%.*s%*s\n", m, s, n-m, s+m);
    >


    Nice method for printing the One word to the right
    extreme and another to the left extreme .

    Karthik Balaguru
    karthikbalaguru, Mar 19, 2009
    #8
  9. karthikbalaguru

    CBFalconer Guest

    Richard Heathfield wrote:
    > CBFalconer said:
    >> karthikbalaguru wrote:
    >>>

    >> ... snip ...
    >>>
    >>> That is, i am expecting the below output using
    >>> any of the format specifier in printf :-

    >>
    >> If you want to know about printf formats and you are using
    >> Windoze I suggest you mount 4dos, n869.txt, Buergs list, and
    >> the following two alias'.

    >
    > He has asked a topical question, and you've introduced plenty of
    > non-topical distractions in your failure to answer his question.
    >
    >> Then the command 'printf' will tell you all you want
    >> to know. Instantaneously. Without cluttering the C newsgroup.

    >
    > Cluttering the C newsgroup with a question about C?


    You may disagree with my suggestion. Fine. It is quite evident
    that you have not noticed balagurus continuous posing of questions
    that are answered in almost any C text. Not once. Apparently
    daily.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
    CBFalconer, Mar 19, 2009
    #9
  10. karthikbalaguru

    JosephKK Guest

    On Wed, 18 Mar 2009 12:53:03 -0700 (PDT), karthikbalaguru
    <> wrote:

    >On Mar 19, 12:47 am, Ben Pfaff <> wrote:
    >> karthikbalaguru <> writes:
    >> > Is ther any specifier to print the Hello World
    >> > such that Hello goes to the left extreme and
    >> > World to the Right extreme with spaces inbetweeen
    >> > them ?

    >>
    >> > That is, i am expecting the below output using
    >> > any of the format specifier in printf :-
    >> > Hello      World

    >>
    >> No, there is no printf specifier to do that.

    >
    >As there is lot of tricks with format specifiers,
    >a trick for the above output can be added into C.
    >I think, there should be some support as below in
    >the future releases of C to do that -
    >printf("%-15.-5.5s\n",s);
    > where the -5 is for having the Hello on
    >the extreme left and the 5 is for having
    >the World on the extreme right.
    >
    >Karthik Balaguru


    Confrakulations, you have made it to the very rare "entertaining
    nutcase" catagory. Membership has historically been proven to be
    transitory.
    .
    JosephKK, Mar 22, 2009
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. news.frontiernet.net
    Replies:
    6
    Views:
    1,108
    news.frontiernet.net
    Apr 16, 2004
  2. loveNUNO
    Replies:
    2
    Views:
    896
    loveNUNO
    Nov 20, 2003
  3. =?Utf-8?B?QmlzaG95?=
    Replies:
    0
    Views:
    967
    =?Utf-8?B?QmlzaG95?=
    Dec 28, 2006
  4. Replies:
    13
    Views:
    804
    santosh
    Oct 4, 2007
  5. Oran
    Replies:
    2
    Views:
    518
Loading...

Share This Page