Re: cannot understand strrev implementation

From: Robert Stankowic (pcdoktor_at_netway.at)
Date: 11/09/03


Date: Sun, 9 Nov 2003 07:43:54 +0100


"tuchka" <iazva@hotmail.com> schrieb im Newsbeitrag
news:5fe034e4.0311082147.1cb78ec6@posting.google.com...
> Hi, guys!
> I am very new here and just started to learn C. I have previous java
> exp. however.
> I'm abs. stuck on pointers and i'm unable comprehend algorithm of
> simple program that reverses chars in string no matter how long I'm
> staring at it.

OK, here is an attempt to explain how it works

Be aware, that given
char *something = string;
++something; yields the value of something + 1 and increments something,
while
something++; yields the original value of something and increments something

--something; yields the value of something - 1 and decrements something,
while
something--; yields the original value of something and decrements something

#include <stdlib.h>
#include <stdio.h>

char *my_strrev(char *string)
{
   char *original = string;
   char *forward = string;
   char temp;

   /*string points to the first character ('h' in our example)*/
   while(*string)/* *string yields the content of the location where string
points*/
   {
      string++;
      /*this makes string point to the next character,
      and the loop is repeated until the terminating '\0' is found
      ^ shows where string points during the loop
      'h' 'e' 'l' 'l' 'o' '\0'
       ^
      'h' 'e' 'l' 'l' 'o' '\0'
           ^
      'h' 'e' 'l' 'l' 'o' '\0'
               ^
      'h' 'e' 'l' 'l' 'o' '\0'
                   ^
      'h' 'e' 'l' 'l' 'o' '\0'
                       ^
      'h' 'e' 'l' 'l' 'o' '\0'
                            ^
                            *string is now 0,
      which terminates the loop
      */
   }
   /*string now points to the terminating '\0'
   (immidiately after the 'o' in "hello"), and forward still points to
   the beginning of the string (the 'h')*/
   while(forward < string)/*this loop decrements string and increments
forward
                          until the pointers meet in the middle of the
                          original string*/
   {
      /*now our pointers point as shown below
      'h' 'e' 'l' 'l' 'o' '\0'*/
       ^ ^
     temp=*(--string);
      *string=*forward;
      *forward++=temp;
      /*the statements above swap the contents of where (string - 1)
      and forward point. the result, step by step is:
      pass 1
      --string points to the 'o' and decrements string:
      'h' 'e' 'l' 'l' 'o' '\0'
       ^ ^
      *(--string) is 'o', *forward is 'h'
         temp becomes 'o', *string becomes 'h', *forward becomes 'o'
         string is now "oellh"
      pass 2
      'o' 'e' 'l' 'l' 'h' '\0'
           ^ ^
      *(--string) is 'l', *forward is 'e'
         temp becomes 'l', *string becomes 'e', *forward becomes 'l'
         string is now "olleh"
      now we have
      'o' 'l' 'l' 'e' 'h' '\0'
               ^^
      and string as well as forward point to the same location - we are
done*/
   }
   return (original);
}

int main(void)
{
   char string[] = "hello";

   puts(my_strrev(string));
   return EXIT_SUCCESS;
}

HTH
Robert



Relevant Pages

  • Re: How to add thousand separators
    ... First, this code is obsolete as written, because char is a dead data type and should not ... Note that both of these should be stored as string resources since they might need to be ... 18 digits for any reason. ... you have made a VERY SERIOUS DESIGN ERROR. ...
    (microsoft.public.vc.mfc)
  • Re: Returning a character buffer from a DLL
    ... I need to return a string buffer from the DLL in a RunQuery function. ... I find it odd that you are using the obsolete 'char *' data type here. ... want to use a string pointer of any type here! ...
    (microsoft.public.vc.mfc)
  • Re: what is the best way of passing floats into a string
    ... I do not null-terminate as snprintf takes care of this (according to ... But the easiest way to determine the size needed to format a number, ... int length_of_representation(double n,const char* format){ ... I get a nice result of -10.000000 in my char * string. ...
    (comp.unix.programmer)
  • Re: weird problem
    ... I already told you that the comparison between an integer and a float ... to strcmpwhich expects a pointer to a string. ... And now a question about something else: why do you use floating ... int,float, char, etc. ...
    (comp.lang.c)
  • Re: why I can not write to the file after initialize the MFC in a service program
    ... you don't use char, an obsolete data type ... Why do you need an intermedate buffer to write literal strings anyway? ... For example, if AfxWinInit fails, you copy a 45-character string into a ... So you are going to try to initialize MFC EACH TIME THROUGH THE LOOP? ...
    (microsoft.public.vc.mfc)