From 6f8eb0f0acb8652afb8771b8fd0d6e723735f774 Mon Sep 17 00:00:00 2001 From: Sandipsinh Rathod Date: Thu, 10 Oct 2024 20:06:06 -0400 Subject: [PATCH] enhance swapChar --- main.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/main.cpp b/main.cpp index 70a5da7..8419513 100644 --- a/main.cpp +++ b/main.cpp @@ -100,9 +100,12 @@ bool isValidPlayer(char c) { } void swapChar(char &a, char &b) { - char temp = a; - a = b; - b = temp; + if (a == b) { + return; + } + a ^= b; + b ^= a; + a ^= b; } // Recursively reverse a string @@ -112,6 +115,7 @@ void reverseString(string &str, int start, int end) { } swapChar(str[start], str[end]); + reverseString(str, start + 1, end - 1); }