Method1:
char *p1, *p2;
: Declare two pointersp1
andp2
to traverse the string.if (!str || !*str) return str;
: This part checks if the input stringstr
is eitherNULL
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.The
for
loop iterates over the string, swapping characters using bitwise XOR operations:p1 = str, p2 = str + strlen(str) - 1;
: Initializep1
to the start of the string andp2
to the end of the string (excluding the null terminator).p2 > p1;
: The loop condition checks ifp2
is greater thanp1
, which means there are still characters left to swap.++p1, --p2
: Incrementp1
and decrementp2
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 byp1
andp2
.*p2 ^= *p1;
: Bitwise XOR between the characters pointed to byp2
andp1
.*p1 ^= *p2;
: Bitwise XOR between the characters pointed to byp1
andp2
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
Comments
Post a Comment