How to left pad a string with spaces?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nospam

    How to left pad a string with spaces?

    If I have a string say:

    myvar[128] = "This is a string";



    How do I use sprintf to convert the string so it has 4 spaces padded on
    the left like:



    " This is a string";


    Thanks!
  • Ben Bacarisse

    #2
    Re: How to left pad a string with spaces?

    nospam <no@spam.comwri tes:
    If I have a string say:
    >
    myvar[128] = "This is a string";
    >
    How do I use sprintf to convert the string so it has 4 spaces padded on
    the left like:
    >
    " This is a string";
    If you mean "in place" -- the result ending up in the same array -- then
    you can't do it directly because sprintf is undefined if used with
    overlapping objects.

    Using memmove and memset is, IMO, the simplest way to do this.

    --
    Ben.

    Comment

    • Jens Thoms Toerring

      #3
      Re: How to left pad a string with spaces?

      nospam <no@spam.comwro te:
      If I have a string say:
      myvar[128] = "This is a string";
      I guess that's supposed to be

      char myvar[128] = "This is a string";
      How do I use sprintf to convert the string so it has 4 spaces padded on
      the left like:
      " This is a string";
      sprint() doesn't convert strings, it prints into a string.
      It looks as if you would like to use sprintf() with 'myvar'
      as both the source and the destination and that's not pos-
      sible. The C standard says about this specifically:

      If copying takes place between objects that overlap,
      the behavior is undefined.

      which exactly addresses this situation. So there's no way
      you can do this reliably with sprintf().

      I guess in the end you want to do something more complicated
      but for what you describe you want to do a simple

      memmove( myvar + 4, myvar, strlen( myvar ) + 1 );
      memset( myvar, ' ', 4 );

      will do. Note the use of memmove() instead of memcpy() which
      is required since source and destination overlap.

      Regards, Jens
      --
      \ Jens Thoms Toerring ___ jt@toerring.de
      \______________ ____________ http://toerring.de

      Comment

      • nospam

        #4
        Re: How to left pad a string with spaces?

        On Mon, 09 Jun 2008 23:28:17 +0000, Jens Thoms Toerring wrote:
        nospam <no@spam.comwro te:
        >If I have a string say:
        >
        >myvar[128] = "This is a string";
        >
        I guess that's supposed to be
        >
        char myvar[128] = "This is a string";
        >
        >How do I use sprintf to convert the string so it has 4 spaces padded on
        >the left like:
        >
        >" This is a string";
        >
        sprint() doesn't convert strings, it prints into a string.
        It looks as if you would like to use sprintf() with 'myvar'
        as both the source and the destination and that's not pos-
        sible. The C standard says about this specifically:
        >
        If copying takes place between objects that overlap,
        the behavior is undefined.
        >
        which exactly addresses this situation. So there's no way
        you can do this reliably with sprintf().
        >
        I guess in the end you want to do something more complicated
        but for what you describe you want to do a simple
        >
        memmove( myvar + 4, myvar, strlen( myvar ) + 1 );
        memset( myvar, ' ', 4 );
        >
        will do. Note the use of memmove() instead of memcpy() which
        is required since source and destination overlap.
        >
        Regards, Jens

        I need to use sprintf, similar to the legacy code I'm working with.
        Something like this:

        strncpy(fname,t mp+21,9); fname[9] = '\0';
        strncpy(mname,t mp+31,1); mname[1] = '\0';

        sprintf(tmp,"%4 s%-20s%-20s%-32s%-4s",
        " ",fname,mname,l name," ");



        Comment

        • nospam

          #5
          Re: How to left pad a string with spaces?

          On Tue, 10 Jun 2008 00:17:45 +0100, Ben Bacarisse wrote:
          nospam <no@spam.comwri tes:
          >
          >If I have a string say:
          >>
          >myvar[128] = "This is a string";
          >>
          >How do I use sprintf to convert the string so it has 4 spaces padded on
          >the left like:
          >>
          >" This is a string";
          >
          If you mean "in place" -- the result ending up in the same array -- then
          you can't do it directly because sprintf is undefined if used with
          overlapping objects.
          >
          Using memmove and memset is, IMO, the simplest way to do this.


          I need to use sprintf, similar to the legacy code I'm working with.
          Something like this:

          strncpy(fname,t mp+21,9); fname[9] = '\0';
          strncpy(mname,t mp+31,1); mname[1] = '\0';

          sprintf(tmp,"%4 s%-20s%-20s%-32s%-4s",
          " ",fname,mname,l name," ");

          Comment

          • CBFalconer

            #6
            Re: How to left pad a string with spaces?

            nospam wrote:
            >
            If I have a string say:
            >
            myvar[128] = "This is a string";
            >
            How do I use sprintf to convert the string so it has 4 spaces
            padded on the left like:
            >
            " This is a string";
            For example, assuming you know s has space for the extra chars:

            void sstretch(char *s, int amount) {
            char *p1, *p2;

            if (amount) {
            pi = strchr(s, '\0');
            if ((p1 s) && amount) {
            p2 = p1 + amount;
            do { /* move the string */
            *p2-- = *p1--;
            } while {p1 s);
            }
            while (amount) { /* inject the blanks */
            *s++ = ' ';
            amount--;
            }
            }
            } /* untested */

            --
            [mail]: Chuck F (cbfalconer at maineline dot net)
            [page]: <http://cbfalconer.home .att.net>
            Try the download section.


            ** Posted from http://www.teranews.com **

            Comment

            • Ben Bacarisse

              #7
              Re: How to left pad a string with spaces?

              nospam <no@spam.comwri tes:
              On Tue, 10 Jun 2008 00:17:45 +0100, Ben Bacarisse wrote:
              >
              >nospam <no@spam.comwri tes:
              >>
              >>If I have a string say:
              >>>
              >>myvar[128] = "This is a string";
              >>>
              >>How do I use sprintf to convert the string so it has 4 spaces padded on
              >>the left like:
              >>>
              >>" This is a string";
              >>
              >If you mean "in place" -- the result ending up in the same array -- then
              >you can't do it directly because sprintf is undefined if used with
              >overlapping objects.
              >>
              >Using memmove and memset is, IMO, the simplest way to do this.
              >
              >
              >
              I need to use sprintf, similar to the legacy code I'm working with.
              Something like this:
              Why do you *need* to do it any particular way? I suspect we mean
              different things by "need".
              strncpy(fname,t mp+21,9); fname[9] = '\0';
              strncpy(mname,t mp+31,1); mname[1] = '\0';
              >
              sprintf(tmp,"%4 s%-20s%-20s%-32s%-4s",
              " ",fname,mname,l name," ");
              If you *must* use sprintf, what would constitute use of sprintf?
              Would this count?

              memmove(myvar + 4, myvar, strlen(myvar) + 1);
              sprintf(myvar, " ");
              myvar[3] = ' ';

              what about:

              memmove(myvar + 4, myvar, strlen(myvar) + 1);
              memset(myvar, ' ', 4);
              sprintf(myvar + strlen(myvar), "");

              ?

              All of this assumes that you need it done in-place. It is just
              possible that what you are asking for is:

              sprintf(tmp, " %s", myvar);

              but that seems so obvious as to be an unlikely question.

              --
              Ben.

              Comment

              • Ben Bacarisse

                #8
                Re: How to left pad a string with spaces?

                CBFalconer <cbfalconer@yah oo.comwrites:
                nospam wrote:
                >>
                >If I have a string say:
                >>
                >myvar[128] = "This is a string";
                >>
                >How do I use sprintf to convert the string so it has 4 spaces
                >padded on the left like:
                >>
                >" This is a string";
                >
                For example, assuming you know s has space for the extra chars:
                >
                void sstretch(char *s, int amount) {
                char *p1, *p2;
                >
                if (amount) {
                pi = strchr(s, '\0');
                if ((p1 s) && amount) {
                p2 = p1 + amount;
                do { /* move the string */
                *p2-- = *p1--;
                } while {p1 s);
                }
                while (amount) { /* inject the blanks */
                *s++ = ' ';
                amount--;
                }
                }
                } /* untested */
                Yuck. Two typos and two logic errors. You complain about not posting
                corrections, so the correction is:

                void sstretch(char *s, size_t amount)
                {
                memmove(s + amount, s, strlen(s) + 1);
                memset(s, ' ', amount);
                }

                Why would you do all those gymnastics with the potential (so well
                demonstrated) for errors?

                --
                Ben.

                Comment

                • Richard

                  #9
                  Re: How to left pad a string with spaces?

                  Ben Bacarisse <ben.usenet@bsb .me.ukwrites:
                  CBFalconer <cbfalconer@yah oo.comwrites:
                  >
                  >nospam wrote:
                  >>>
                  >>If I have a string say:
                  >>>
                  >>myvar[128] = "This is a string";
                  >>>
                  >>How do I use sprintf to convert the string so it has 4 spaces
                  >>padded on the left like:
                  >>>
                  >>" This is a string";
                  >>
                  >For example, assuming you know s has space for the extra chars:
                  >>
                  > void sstretch(char *s, int amount) {
                  > char *p1, *p2;
                  >>
                  > if (amount) {
                  > pi = strchr(s, '\0');
                  > if ((p1 s) && amount) {
                  > p2 = p1 + amount;
                  > do { /* move the string */
                  > *p2-- = *p1--;
                  > } while {p1 s);
                  > }
                  > while (amount) { /* inject the blanks */
                  > *s++ = ' ';
                  > amount--;
                  > }
                  > }
                  > } /* untested */
                  >
                  Yuck. Two typos and two logic errors. You complain about not posting
                  corrections, so the correction is:
                  >
                  void sstretch(char *s, size_t amount)
                  {
                  memmove(s + amount, s, strlen(s) + 1);
                  memset(s, ' ', amount);
                  }
                  >
                  Why would you do all those gymnastics with the potential (so well
                  demonstrated) for errors?
                  I would also point out to the beginner, and "Chuck", to read the man
                  pages and to see why memmove is better than memcpy in this and other
                  situations. What the hell is that horrific mess above the correct
                  solution?!?!


                  Comment

                  • Ben Bacarisse

                    #10
                    Re: How to left pad a string with spaces?

                    Richard<rgrdev@ gmail.comwrites :
                    Ben Bacarisse <ben.usenet@bsb .me.ukwrites:
                    <snip>
                    >void sstretch(char *s, size_t amount)
                    >{
                    > memmove(s + amount, s, strlen(s) + 1);
                    > memset(s, ' ', amount);
                    >}
                    >>
                    >
                    I would also point out to the beginner, and "Chuck", to read the man
                    pages and to see why memmove is better than memcpy in this and other
                    situations.
                    That is an odd way of putting it. memmove is *essential* in this
                    situation. In other situations memcpy may be better. I did not
                    explain because someone else has already done so.

                    --
                    Ben.

                    Comment

                    • Richard

                      #11
                      Re: How to left pad a string with spaces?

                      Ben Bacarisse <ben.usenet@bsb .me.ukwrites:
                      Richard<rgrdev@ gmail.comwrites :
                      >
                      >Ben Bacarisse <ben.usenet@bsb .me.ukwrites:
                      <snip>
                      >>void sstretch(char *s, size_t amount)
                      >>{
                      >> memmove(s + amount, s, strlen(s) + 1);
                      >> memset(s, ' ', amount);
                      >>}
                      >>>
                      >>
                      >I would also point out to the beginner, and "Chuck", to read the man
                      >pages and to see why memmove is better than memcpy in this and other
                      >situations.
                      >
                      That is an odd way of putting it. memmove is *essential* in this
                      situation. In other situations memcpy may be better. I did not
                      explain because someone else has already done so.
                      I didnt see any other post on the matter. "better" is mentioned to get
                      people to read the man pages. Self help and all that. I see nothing odd
                      at all in mentioning it as "better".

                      Comment

                      • Ben Bacarisse

                        #12
                        Re: How to left pad a string with spaces?

                        Richard<rgrdev@ gmail.comwrites :
                        Ben Bacarisse <ben.usenet@bsb .me.ukwrites:
                        >
                        >Richard<rgrdev @gmail.comwrite s:
                        >>
                        >>Ben Bacarisse <ben.usenet@bsb .me.ukwrites:
                        ><snip>
                        >>>void sstretch(char *s, size_t amount)
                        >>>{
                        >>> memmove(s + amount, s, strlen(s) + 1);
                        >>> memset(s, ' ', amount);
                        >>>}
                        >>>>
                        >>>
                        >>I would also point out to the beginner, and "Chuck", to read the man
                        >>pages and to see why memmove is better than memcpy in this and other
                        >>situations.
                        >>
                        >That is an odd way of putting it. memmove is *essential* in this
                        >situation. In other situations memcpy may be better. I did not
                        >explain because someone else has already done so.
                        >
                        I didnt see any other post on the matter.
                        Message ID: <6b5skhF3ant6cU 1@mid.uni-berlin.de Jens Thoms Toerring:

                        "Note the use of memmove() instead of memcpy() which is required
                        since source and destination overlap."

                        --
                        Ben.

                        Comment

                        • pete

                          #13
                          Re: How to left pad a string with spaces?

                          nospam wrote:
                          If I have a string say:
                          >
                          myvar[128] = "This is a string";
                          >
                          >
                          >
                          How do I use sprintf to convert the string so it has 4 spaces padded on
                          the left like:
                          >
                          >
                          >
                          " This is a string";

                          /* BEGIN new.c */

                          #include <stdio.h>

                          int main(void)
                          {
                          char myvar[128] = "This is a string";

                          sprintf(myvar, "%20s", "This is a string");
                          puts(myvar);
                          return 0;
                          }

                          /* END new.c */

                          --
                          pete

                          Comment

                          • rahul

                            #14
                            Re: How to left pad a string with spaces?

                            On Jun 10, 7:57 am, Richard<rgr...@ gmail.comwrote:
                            Ben Bacarisse <ben.use...@bsb .me.ukwrites:
                            CBFalconer <cbfalco...@yah oo.comwrites:
                            >
                            nospam wrote:
                            >
                            >If I have a string say:
                            >
                            >myvar[128] = "This is a string";
                            >
                            >How do I use sprintf to convert the string so it has 4 spaces
                            >padded on the left like:
                            >
                            >" This is a string";
                            >
                            For example, assuming you know s has space for the extra chars:
                            >
                            void sstretch(char *s, int amount) {
                            char *p1, *p2;
                            >
                            if (amount) {
                            pi = strchr(s, '\0');
                            if ((p1 s) && amount) {
                            p2 = p1 + amount;
                            do { /* move the string */
                            *p2-- = *p1--;
                            } while {p1 s);
                            }
                            while (amount) { /* inject the blanks */
                            *s++ = ' ';
                            amount--;
                            }
                            }
                            } /* untested */
                            >
                            Yuck. Two typos and two logic errors. You complain about not posting
                            corrections, so the correction is:
                            >
                            void sstretch(char *s, size_t amount)
                            {
                            memmove(s + amount, s, strlen(s) + 1);
                            memset(s, ' ', amount);
                            }
                            >
                            Why would you do all those gymnastics with the potential (so well
                            demonstrated) for errors?
                            >
                            I would also point out to the beginner, and "Chuck", to read the man
                            pages and to see why memmove is better than memcpy in this and other
                            situations. What the hell is that horrific mess above the correct
                            solution?!?!
                            Other than the overlapping memory case, how is memmove supposed to be
                            better than memcpy?
                            I am unaware of any other scenario in which memmove is better than
                            memcpy.

                            Comment

                            • CBFalconer

                              #15
                              Re: How to left pad a string with spaces?

                              Ben Bacarisse wrote:
                              CBFalconer <cbfalconer@yah oo.comwrites:
                              >
                              .... snip ...
                              >
                              > void sstretch(char *s, int amount) {
                              > char *p1, *p2;
                              >>
                              > if (amount) {
                              > pi = strchr(s, '\0');
                              > if ((p1 s) && amount) {
                              > p2 = p1 + amount;
                              > do { /* move the string */
                              > *p2-- = *p1--;
                              > } while {p1 s);
                              > }
                              > while (amount) { /* inject the blanks */
                              > *s++ = ' ';
                              > amount--;
                              > }
                              > }
                              > } /* untested */
                              >
                              Yuck. Two typos and two logic errors. You complain about not posting
                              corrections, so the correction is:
                              >
                              void sstretch(char *s, size_t amount)
                              {
                              memmove(s + amount, s, strlen(s) + 1);
                              memset(s, ' ', amount);
                              }
                              One does it for the exercise. I see only one typo [while {p1 >
                              s)]] and one unnecessary test [&& amount]. A compilation may well
                              show up more. What did you see?

                              Incidentally, a speed comparison would be interesting. I don't
                              think your version allows for amount == 0 and for an empty original
                              string. I would have to check the specs for memove and memset with
                              care.

                              --
                              [mail]: Chuck F (cbfalconer at maineline dot net)
                              [page]: <http://cbfalconer.home .att.net>
                              Try the download section.


                              ** Posted from http://www.teranews.com **

                              Comment

                              Working...