String reversing problem

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

    #1

    String reversing problem

    Why doesn't:

    #include <stdio.h>

    void reverse(char[], int);

    main()
    {
    char s[5];

    s[0] = 'h';
    s[1] = 'e';
    s[2] = 'l';
    s[3] = 'l';
    s[4] = 'o';
    reverse(s, 5);

    for (int i=0; i<=4; i++)
    putchar(s[i]);
    return 0;
    }

    void reverse(char s[], int num_elements)
    {
    int i, j;

    for (i=0,j=num_elem ents-1; (i<=num_element s-1) && (j>=0); i++,j--)
    s[i] = s[j];
    }

    output:

    olleh

    ?

  • Artie Gold

    #2
    Re: String reversing problem

    Albert wrote:[color=blue]
    > Why doesn't:
    >
    > #include <stdio.h>
    >
    > void reverse(char[], int);
    >
    > main()
    > {
    > char s[5];
    >
    > s[0] = 'h';
    > s[1] = 'e';
    > s[2] = 'l';
    > s[3] = 'l';
    > s[4] = 'o';
    > reverse(s, 5);
    >
    > for (int i=0; i<=4; i++)
    > putchar(s[i]);
    > return 0;
    > }
    >
    > void reverse(char s[], int num_elements)
    > {
    > int i, j;
    >
    > for (i=0,j=num_elem ents-1; (i<=num_element s-1) && (j>=0); i++,j--)[/color]

    Think what happens when i==4 and j==1, for example. [Do they still call
    it `desk checking'?]
    [color=blue]
    > s[i] = s[j];
    > }
    >
    > output:
    >
    > olleh
    >
    > ?
    >[/color]
    HTH,
    --ag

    --
    Artie Gold -- Austin, Texas
    http://goldsays.blogspot.com (new post 8/5)
    http://www.cafepress.com/goldsays
    "If you have nothing to hide, you're not trying!"

    Comment

    • Diptendra

      #3
      Re: String reversing problem

      use this one
      #include <stdio.h>


      void reverse(char[], int);


      main()
      {
      char s[5];
      int i;

      s[0] = 'h';
      s[1] = 'e';
      s[2] = 'l';
      s[3] = 'l';
      s[4] = 'o';
      reverse(s, 5);


      for ( i = 0; i<=4; i++)
      putchar(s[i]);
      return 0;



      }


      void reverse(char s[], int num_elements)
      {
      int i, j;

      for (i=0,j=num_elem ents-1; (i<=num_element s-1) && (j>=0); i++,j--)
      s[i] = s[j];



      }

      Comment

      • Albert

        #4
        Re: String reversing problem

        What do you mean by 'desk checking'?

        Comment

        • slebetman@yahoo.com

          #5
          Re: String reversing problem

          Albert wrote:[color=blue]
          > What do you mean by 'desk checking'?[/color]

          I think he means checking by pen & paper. I'd call it "checking by pen
          & paper" although I always use a whiteboard for it.

          Computers can only do what you tell them to do. As they say: garbage
          in, garbage out. Sometimes when the computer doesn't do what you want
          it is worth checking if you told it to do what you thought you wanted.

          Comment

          • slebetman@yahoo.com

            #6
            Re: String reversing problem

            Albert wrote:[color=blue]
            > Why doesn't:
            >
            > <snip>
            > s[i] = s[j];
            >[/color]

            This is a good example of why desk-checking is good. Lets take a
            "hello" string shall we? Note that in the "diagram" below I mark the
            variable being "read" from with + and the variable being "written" to
            with ^.

            h e l l o // original string

            o e l l o // doing s[0] = s[4]
            ^ +

            o l l l o // doing s[1] = s[3]
            ^ +

            o l l l o // doing s[2] = s[2]
            ^

            o l l l o // doing s[3] = s[1]
            + ^

            At this point I hope you see the problem with you code since you've
            overwritten the original 'e' with an 'l'.

            Comment

            • pai

              #7
              Re: String reversing problem

              hi ,
              I think an extra variable is needed to store, bcoz u cant
              interchange 2 variable as such.
              or is there any way to do it ...
              Pai

              Comment

              • Richard Heathfield

                #8
                Re: String reversing problem

                pai said:
                [color=blue]
                > hi ,
                > I think an extra variable is needed to store, bcoz u cant
                > interchange 2 variable as such.
                > or is there any way to do it ...[/color]

                There is a way to do this under certain conditions, but it's not a very
                bright idea.

                --
                Richard Heathfield
                "Usenet is a strange place" - dmr 29/7/1999

                email: rjh at above domain (but drop the www, obviously)

                Comment

                • Chuck F.

                  #9
                  Re: String reversing problem

                  pai wrote:[color=blue]
                  >
                  > I think an extra variable is needed to store, bcoz u cant
                  > interchange 2 variable as such. or is there any way to do it
                  >[/color]
                  Include context, without which your message is meaningless. For
                  means on the broken google interface, see my sig below.

                  Try this, after #include <string.h>:

                  /* reverse string in place. Return length */
                  static size_t revstring(char *stg)
                  {
                  char *last, temp;
                  size_t lgh;

                  if ((lgh = strlen(stg)) > 1) {
                  last = stg + lgh; /* points to '\0' */
                  while (last-- > stg) {
                  temp = *stg; *stg++ = *last; *last = temp;
                  }
                  }
                  return lgh;
                  } /* revstring */

                  --
                  "If you want to post a followup via groups.google.c om, don't use
                  the broken "Reply" link at the bottom of the article. Click on
                  "show options" at the top of the article, then click on the
                  "Reply" at the bottom of the article headers." - Keith Thompson
                  More details at: <http://cfaj.freeshell. org/google/>

                  Comment

                  • haroon

                    #10
                    Re: String reversing problem


                    Albert wrote:[color=blue]
                    > Why doesn't:[/color]
                    [...][color=blue]
                    > void reverse(char s[], int num_elements)
                    > {
                    > int i, j;
                    >
                    > for (i=0,j=num_elem ents-1; (i<=num_element s-1) && (j>=0); i++,j--)
                    > s[i] = s[j];
                    > }[/color]

                    try writing reverse(...) like this:

                    /***/
                    void reverse(char s[], int num_elements)
                    {
                    int i, j;
                    char t;

                    for (i=0,j=num_elem ents-1; (i<=(num_elemen ts-1) / 2) && (j>=0);
                    i++,j--)
                    {
                    t = s[i];
                    s[i] = s[j];
                    s[j] = t;
                    }
                    }

                    /***/

                    then analyze both to figure out whats the difference and what happend.

                    Comment

                    • tmp123

                      #11
                      Re: String reversing problem

                      Chuck F. wrote:[color=blue]
                      > pai wrote:[color=green]
                      > >
                      > > I think an extra variable is needed to store, bcoz u cant
                      > > interchange 2 variable as such. or is there any way to do it
                      > >[/color]
                      > Include context, without which your message is meaningless. For
                      > means on the broken google interface, see my sig below.
                      >
                      > Try this, after #include <string.h>:
                      >
                      > /* reverse string in place. Return length */
                      > static size_t revstring(char *stg)
                      > {
                      > char *last, temp;
                      > size_t lgh;
                      >
                      > if ((lgh = strlen(stg)) > 1) {
                      > last = stg + lgh; /* points to '\0' */
                      > while (last-- > stg) {
                      > temp = *stg; *stg++ = *last; *last = temp;
                      > }
                      > }
                      > return lgh;
                      > } /* revstring */
                      >
                      > --
                      > "If you want to post a followup via groups.google.c om, don't use
                      > the broken "Reply" link at the bottom of the article. Click on
                      > "show options" at the top of the article, then click on the
                      > "Reply" at the bottom of the article headers." - Keith Thompson
                      > More details at: <http://cfaj.freeshell. org/google/>[/color]

                      Hi,

                      I agree on the previous, and, of course, if some day I need to code
                      something similar I will write more or less the same (specially in an
                      answer to a beginner).

                      But, just for fun, and taken into account is the third time this
                      question has been posted, another version:

                      int revstring ( char *s )
                      {
                      char *e;
                      int r;
                      for( e=s+(r=strlen(s ))-1; s<e; *s^=*e^=*s^=*e, s++, e--);
                      return r;
                      }


                      Kind regards.

                      (hope google doesn't heats indentation).

                      Comment

                      • William J. Leary Jr.

                        #12
                        Re: String reversing problem

                        "Albert" <albert.xtheunk nown0@gmail.com > wrote in message
                        news:1135915021 .824221.86590@g 43g2000cwa.goog legroups.com...[color=blue]
                        > What do you mean by 'desk checking'?[/color]

                        I'd have called it "paper check," or, when I'm feeling whimsical "let's play
                        computer."

                        Whatever way it's expressed, it means get out some paper and a pen(cil) and
                        perform, yourself, the steps the computer will take to execute your program.
                        Sometimes it's better (or quicker) than a debugger. Sometimes it's the only
                        way to debug (for very limited platforms, for example). Well worth practicing.

                        - Bill


                        Comment

                        • Chuck F.

                          #13
                          Re: String reversing problem

                          tmp123 wrote:[color=blue]
                          > Chuck F. wrote:
                          >[/color]
                          .... snip ...[color=blue]
                          >[color=green]
                          >> /* reverse string in place. Return length */
                          >> static size_t revstring(char *stg)
                          >> {
                          >> char *last, temp;
                          >> size_t lgh;
                          >>
                          >> if ((lgh = strlen(stg)) > 1) {
                          >> last = stg + lgh; /* points to '\0' */
                          >> while (last-- > stg) {
                          >> temp = *stg; *stg++ = *last; *last = temp;
                          >> }
                          >> }
                          >> return lgh;
                          >> } /* revstring */[/color]
                          >[/color]
                          .... snip ...[color=blue]
                          >
                          > But, just for fun, and taken into account is the third time this
                          > question has been posted, another version:
                          >
                          > int revstring ( char *s )
                          > {
                          > char *e;
                          > int r;
                          > for( e=s+(r=strlen(s ))-1; s<e; *s^=*e^=*s^=*e, s++, e--);
                          > return r;
                          > }[/color]

                          FYI your version invokes undefined behaviour. Try it with a string
                          of zero chars. My length test is not there for fun. I also have
                          evil suspicions about the xor operations.

                          --
                          "If you want to post a followup via groups.google.c om, don't use
                          the broken "Reply" link at the bottom of the article. Click on
                          "show options" at the top of the article, then click on the
                          "Reply" at the bottom of the article headers." - Keith Thompson
                          More details at: <http://cfaj.freeshell. org/google/>

                          Comment

                          • tmp123

                            #14
                            Re: String reversing problem

                            Chuck F. wrote:[color=blue]
                            > tmp123 wrote:[color=green]
                            > > int revstring ( char *s )
                            > > {
                            > > char *e;
                            > > int r;
                            > > for( e=s+(r=strlen(s ))-1; s<e; *s^=*e^=*s^=*e, s++, e--);
                            > > return r;
                            > > }[/color]
                            >
                            > FYI your version invokes undefined behaviour. Try it with a string
                            > of zero chars. My length test is not there for fun. I also have
                            > evil suspicions about the xor operations.
                            >[/color]

                            Hi,

                            If length is 0, then e=s-1, thus s<e is false exiting loop.

                            However, if the usage of pointer comparation and pointer substraction
                            is not welcome, another version:

                            void revstring ( char *s )
                            {
                            char *e;
                            for( e=s+strlen(s); s!=e-- && s!=e; *s^=*e^=*s++^=* e);
                            }

                            Kind regards.

                            PS: some days ago, someone posted about if "C is easy". I do not known,
                            but it is tricky.

                            Comment

                            • Keith Thompson

                              #15
                              Re: String reversing problem

                              "tmp123" <tmp123@menta.n et> writes:
                              [...][color=blue]
                              > void revstring ( char *s )
                              > {
                              > char *e;
                              > for( e=s+strlen(s); s!=e-- && s!=e; *s^=*e^=*s++^=* e);
                              > }[/color]

                              Undefined behavior.
                              [color=blue]
                              > PS: some days ago, someone posted about if "C is easy". I do not known,
                              > but it is tricky.[/color]

                              It can be if you go out of your way to make it tricky.

                              --
                              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                              San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                              We must do something. This is something. Therefore, we must do this.

                              Comment

                              Working...