How to Swap Two Strings Without Using Third String in C++? Last Updated : 29 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, a string is a sequence of characters. Swapping two strings typically involves using a third string as temporary storage. However, it’s possible to swap two strings without using a third string. In this article, we will learn how to do this in C++. Example Input: str1 = "Hello" str2 = "world" Output: str1 = "world" str2 = "Hello"Swapping Strings Without Using a Third StringThe XOR algorithm can be used to swap two strings without using a third string. The XOR operation has the property that A ^ B ^ B = Awhich can be used to swap two variables. ApproachIterate through each character of the strings.Perform bitwise XOR (^) operation on corresponding characters of the strings.Repeat the operation for all characters, effectively swapping their values.C++ Program to Swap Strings Without Using Third String C++ // C++ Program to illustrate how to swap strings without // using a third-string #include <iostream> #include <string> using namespace std; // function to SWap two strings Without Using Third Variable void swapStrings(string& str1, string& str2) { int length1 = str1.length(); int length2 = str2.length(); for (int i = 0; i < length1 && i < length2; ++i) { str1[i] ^= str2[i]; str2[i] ^= str1[i]; str1[i] ^= str2[i]; } if (length1 != length2) { for (int i = length1; i < length2; ++i) { str1 += str2[i]; } for (int i = length2; i < length1; ++i) { str2 += str1[i]; } str1 = str1.substr(0, length2); str2 = str2.substr(0, length1); } } // Driver Code int main() { string str1 = "Hello"; string str2 = "World"; cout << "Before swapping:" << endl; cout << "String 1: " << str1 << endl; cout << "String 2: " << str2 << endl; swapStrings(str1, str2); cout << "\nAfter swapping:" << endl; cout << "String 1: " << str1 << endl; cout << "String 2: " << str2 << endl; return 0; } OutputBefore swapping: String 1: Hello String 2: World After swapping: String 1: World String 2: Hello Time Complexity: O(max(N,M)), where N and M are the sizes of the strings.Space Complexity: O(1) Comment More infoAdvertise with us Next Article How to Swap Two Strings Without Using Third String in C++? A anuragvbj79 Follow Improve Article Tags : C++ Programs C++ STL cpp-string CPP Examples +1 More Practice Tags : CPPSTL Similar Reads Swap Two Numbers Without Third Variable in C++ In C++, swapping two numbers means we need to exchange the value of two numbers. In this article, we will learn how to swap two numbers without using the third variable in C++. Example Input: a=10b=20Output:After swapping:a=20b=10Swap Two Numbers Without Using a Third VariableIn C++ we can swap two 2 min read How to Swap Two Numbers Using Pointers in C++? Swapping the values of two numbers is a very common operation in programming, which is often used to understand the basics of variables, pointers, and function calls. In this article, we will learn how to swap two numbers using pointers in C++. Example Input:int x=10;int y=20;Output:int x=20;int y=1 3 min read How to Replace Text in a String Using Regex in C++? Regular expressions or what we can call regex for short are sequences of symbols and characters that create a search pattern and help us to find specific patterns within a given text. In this article, we will learn how to replace text in a string using regex in C++. Example Input: str = "Hello, Worl 2 min read Swap Two Numbers using Function in C++ Swapping numbers means exchanging the values of the two numbers with each other. For Example, Before Swapping:a = 10, b = 22;After swapping:a = 22, b = 10In this article, we will write a program to swap two numbers using a function in C++. How to Swap Two Numbers Using Function in C++?We will pass t 3 min read How to Reverse a String in C++? Reversing a string means replacing the first character with the last character, second character with the second last character and so on. In this article, we will learn how to reverse a string in C++.ExamplesInput: str = "Hello World"Output: dlroW olleHExplanation: The last character is replaced by 2 min read Like