Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use either of the two methods below, depending on whether you're comfortable with pointers or not. It will also be worthwhile looking at them side-by-side when you satrt learning about pointers so you can better understand how they map to each other.</p> <p>This is a full program for testing purposes:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;string.h&gt; // The pointer version. void reverse1 (char *str) { char t; // Temporary char for swapping. char *s = str; // First character of string. char *e = &amp;(s[strlen(s)-1]); // Last character of string. // Swap first and last character the move both pointers // towards each other. Stop when they meet or cross. while (s &lt; e) { t = *s; *s++ = *e; *e-- = t; } } // The array version. void reverse2 (char *str) { char t; // Temporary char for swapping. int s = 0; // First character of string. int e = strlen(str)-1; // Last character of string. // Swap first and last character the move both pointers // towards each other. Stop when they meet or cross. while (s &lt; e) { t = str[s]; str[s++] = str[e]; str[e--] = t; } } </code></pre> <p>&nbsp;</p> <pre><code>int main (void) { char x[] = "This is a string for reversing."; printf ("Original: [%s]\n", x); reverse1 (x); printf ("Reversed: [%s]\n", x); reverse2 (x); printf (" Again: [%s]\n", x); return 0; } </code></pre> <p>and the output is:</p> <pre><code>Original: [This is a string for reversing.] Reversed: [.gnisrever rof gnirts a si sihT] Again: [This is a string for reversing.] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload