Method1: char *p1, *p2; : Declare two pointers p1 and p2 to traverse the string. 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. 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 point...
Learn programming, data structures, and algorithms on CoderEdge with simple tutorials, coding examples, and guides for students and developers.