Re: cannot understand strrev implementation
From: Robert Stankowic (pcdoktor_at_netway.at)
Date: 11/09/03
- Next message: Richard Heathfield: "Re: cannot understand strrev implementation"
- Previous message: Richard Heathfield: "Re: "Mastering C Pointers"...."
- In reply to: tuchka: "cannot understand strrev implementation"
- Next in thread: Richard Heathfield: "Re: cannot understand strrev implementation"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Richard Heathfield: "Re: cannot understand strrev implementation"
- Previous message: Richard Heathfield: "Re: "Mastering C Pointers"...."
- In reply to: tuchka: "cannot understand strrev implementation"
- Next in thread: Richard Heathfield: "Re: cannot understand strrev implementation"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|