Skip to main content

strrev() function in C

Method1:


  1. char *p1, *p2;: Declare two pointers p1 and p2 to traverse the string.

  2. if (!str || !*str) return str;: This part checks if the input string str is either NULL or an empty string. If either condition is true, the function simply returns the input string as there's no need to reverse an empty or nonexistent string.

  3. The for loop iterates over the string, swapping characters using bitwise XOR operations:

    • p1 = str, p2 = str + strlen(str) - 1;: Initialize p1 to the start of the string and p2 to the end of the string (excluding the null terminator).

    • p2 > p1;: The loop condition checks if p2 is greater than p1, which means there are still characters left to swap.

    • ++p1, --p2: Increment p1 and decrement p2 to move towards the center of the string.

    • The swapping is performed using bitwise XOR operations:

      • *p1 ^= *p2;: Bitwise XOR between the characters pointed to by p1 and p2.


        *p2 ^= *p1;: Bitwise XOR between the characters pointed to by p2 and p1.


        *p1 ^= *p2;: Bitwise XOR between the characters pointed to by p1 and p2 again. This effectively swaps the values.


        After the loop completes, the function returns the modified input string.


        This implementation uses the properties of the bitwise XOR operation to swap two values without the need for a temporary variable. It's a more compact way to achieve the reversal of a string in place. However, it's important to note that this approach might be less intuitive and less readable than the traditional swapping method.



         

         #include <stdio.h>

         #include <string.h>

        char *strrev(char *str)

        {

              char *p1, *p2;


              if (! str || ! *str)

                    return str;

              for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)

              {

                    *p1 ^= *p2;

                    *p2 ^= *p1;

                    *p1 ^= *p2;

              }

              return str;

         

        }

        int main(void) {

            char a[] = "hello world";

            printf("%s", strrev(a));

            return 0;

        }

        Output Code

        dlrow olleh

        Method 2

        #include <stdio.h>

        #include <stdlib.h>


        char *strrev(char *str) {

            int n = strlen(str);

            char strr[n + 1]; // Include space for null terminator

            int j = n - 1; // Start j from n - 1


            for (int i = 0; i < n; i++) {

                strr[i] = str[j];

                j--;

            }

            strr[n] = '\0'; // Null-terminate the reversed string

            strcpy(str, strr);

            return str;

        }


        int main(void) {

            char a[] = "hello world";

            printf("%s", strrev(a));

            return 0;

        }

        Output

        dlrow olleh

        Reference website: 


        https://stackoverflow.com/questions/8534274/is-the-strrev-function-not-available-in-linux*p1 ^= *p2;: Bitwise XOR between the characters pointed to by p1 and p2.

      • *p2 ^= *p1;: Bitwise XOR between the characters pointed to by p2 and p1.

      • *p1 ^= *p2;: Bitwise XOR between the characters pointed to by p1 and p2 again. This effectively swaps the values.

  4. After the loop completes, the function returns the modified input string.

This implementation uses the properties of the bitwise XOR operation to swap two values without the need for a temporary variable. It's a more compact way to achieve the reversal of a string in place. However, it's important to note that this approach might be less intuitive and less readable than the traditional swapping method.

 
 #include <stdio.h>
 #include <string.h>
char *strrev(char *str)
{
      char *p1, *p2;

      if (! str || ! *str)
            return str;
      for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
      {
            *p1 ^= *p2;
            *p2 ^= *p1;
            *p1 ^= *p2;
      }
      return str;
 
}
int main(void) {
    char a[] = "hello world";
    printf("%s", strrev(a));
    return 0;
}

Output Code

dlrow olleh

Method 2

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

char *strrev(char *str) {
    int n = strlen(str);
    char strr[n + 1]; // Include space for null terminator
    int j = n - 1; // Start j from n - 1

    for (int i = 0; i < n; i++) {
        strr[i] = str[j];
        j--;
    }
    strr[n] = '\0'; // Null-terminate the reversed string
    strcpy(str, strr);
    return str;
}

int main(void) {
    char a[] = "hello world";
    printf("%s", strrev(a));
    return 0;
}

Output

dlrow olleh

Reference website: 

https://stackoverflow.com/questions/8534274/is-the-strrev-function-not-available-in-linux

Comments