rstring.c - In-place ASCII string reversal

C code posted by Troy J. Farrell
created at 21 Sep 23:35

Edit | Back
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* Troy J. Farrell <troy at entheossoft dot com>
 * MIT license
 */

#include <stdio.h>
#include <string.h>

/* prototype our function */

int reverseString( char * );

int main( int argc, char **argv ) {

  char s[] = "Quick zephyrs blow, vexing daft Jim.";

  printf( "%s\n", s );
  if( ! reverseString( s ) ) printf( "Success!\n" );
  printf( "%s\n", s );
  return 0;
} /* main */

int reverseString( char *some_string ) {
  char a;      /* temp storage for a character in transit */
  char *p, *q; /* p and q point to the middle of the string
                * and work their way outward in the while
                * loop below.
                */
  int l = strlen( some_string );
  int i = l / 2; /* The while loop iterates once for
                  * every two characters in the string.
                  */

  p = some_string + i; /* point to the "front" of the middle:
                        *  alphabets\0    alphabet\0 
                        *      ^              ^
                        *      p              p
                        */
  q = p--;             /* point to the back of the middle:
                        *  alphabets\0    alphabet\0 
                        *     ^^             ^^
                        *     pq             pq
                        *
                        * notice that p is decremented
                        * after q is assigned.
                        */

  if( l % 2 ) q++;    /* if q points to a middle character
                        * which will not move, then move it
                        * on to the next character.
                        *  alphabets\0    alphabet\0 
                        *     ^ ^            ^^
                        *     p q            pq
                        *
                        * If we skip this step, then the last
                        * letter doesn't get switched, or we
                        * could try to loop one more time and
                        * move the \0 terminator to the start
                        * of some_string :)
                        */
  while( i > 0 ) {
    /*
     * We're walking outward from the middle of the string:
     *
     *  alphabets\0    alphabet\0
     *     ^ ^            ^^
     *  alpbahets\0    alpahbet\0
     *    ^   ^          ^  ^
     *  alebahpts\0    albahpet\0 
     *   ^     ^        ^    ^
     *  atebahpls\0    aebahplt\0 
     *  ^       ^      ^      ^
     *  stebahpla\0    aebahplt\0 
     * ^         ^^    ^       ^^
     * BREAK - no switch happens here since i == 0 at this point
     */
    a = *p;        /* save *p */
    *p = *q;       /* copy *q to *p */
    *q = a;        /* move what was *p to *q */
    p--; q++; i--; /* move backward, forward, and outward in that order */
  } /* while */
  return 0;
} /* reverseString */
2.84 KB in 4 ms with coderay