C-String reverse

mvmalderen 0 Tallied Votes 1K Views Share

A small function which reverses a c-string by just swapping the elements :) ...

Usage::

char[] str = "hello";
reverse(str); // str now contains "olleh"
/**********************************
* Written by Mathias Van Malderen *
**********************************/

void reverse(char p[])
{
     int len=strlen(p);
     char t;
     for(int i=(--len), j=0; i>len/2; i--, j++)
     {
          // exchange elements
          t=p[i];
          p[i]=p[j];
          p[j]=t;
     }
}
MosaicFuneral 812 Nearly a Posting Virtuoso

For C style strings there is <string.h>'s _strrev() and for C++'s strings there's <algorithm>'s reverse().
_strrev() is generally provided in most compilers.

mvmalderen 2,072 Postaholic

>_strrev() is generally provided in most compilers.
But not on all :P

mvmalderen 2,072 Postaholic

This is how it would look like if you asked me to rewrite it:

void reverse(char *p)
{
    char *q = p + strlen(p) - 1;

    for(; p < q; p++, q--)
    {
        char c = *p;
        *p = *q;
        *q = c;
    }
}

(no NULL-pointer check included).

rajeshvari -1 Newbie Poster

make program in c language by using for loop to show
3
323
32123
323
3

mvmalderen commented: Make it yourself! -1
vitkaodessit 0 Newbie Poster

I would do this a bit different:

void reverse(char p[])
{
      int len=strlen(p);

      for(int i=0, i<len/2; i++)
     {
          // exchange elements
          p[i]^=p[len - i - 1];
          p[len - i - 1]^=p[i];
          p[i]^=p[len - i - 1];
      }
}
csurfer 422 Posting Pro

Your code snippet can be shortened tux...here it goes:

void reverse(char p[])
{
     int len=strlen(p);
     for(int i=len-1, j=0;j<i; i--, j++)
     {
          // exchange elements
          p[i]^=p[j]^=p[i]^=p[j];
     }
}

Try it and do tell me what do you think...???

csurfer 422 Posting Pro

Sorry about the reply... I didn't see it was a 6 month old thread resurrected... just answered it... ;)

vitkaodessit 0 Newbie Poster

All I wanted just reduce the number of local variables.
Your line “p^=p[j]^=p^=p[j];” in most of compilers has “undefined behaviour”.
Just try to swap to integers a = 0x55 and b = 0xAA your way (a ^= b ^= a ^= b;) using different compilers.

mvmalderen 2,072 Postaholic

>Sorry about the reply... I didn't see it was a 6 month old thread resurrected... just answered it...
Never mind, any input is welcome.

>All I wanted just reduce the number of local variables.
Yes, I see, though the algorithm of reversing the string is basically the same, I hadn't thought of the the fact that I could use XOR-operations to swap the values without having to use a temporary variable.

Nice suggestion :)

@vitkaodessit: please post your code using code tags the next time.

Nick Evan 4,005 Industrious Poster Team Colleague Featured Poster

>>All I wanted just reduce the number of local variables.
>Yes, I see, though the algorithm of reversing the string is basically the same,
>I hadn't thought of the the fact that I could use XOR-operations to swap the values without having to use a temporary variable.

>Nice suggestion

Although this eliminates the extra variable, it can cause undefined behavior. For some reason, some teachers insist on learning their student this 'xor swap method', but I would strongly advice against it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.