Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Swap two Strings without using temp variable in C#
To swap two strings without using a temporary variable in C#, you can use string concatenation and the Substring() method. This technique works by combining both strings into one, then extracting the original values in reverse order.
Algorithm
The swapping process follows these three steps −
Step 1: Append the second string to the first string.
Step 2: Extract the original first string from the beginning and assign it to the second string variable.
Step 3: Extract the original second string from the remaining part and assign it to the first string variable.
Syntax
Following is the syntax for swapping strings without a temporary variable −
str1 = str1 + str2; str2 = str1.Substring(0, str1.Length - str2.Length); str1 = str1.Substring(str2.Length);
Example
using System;
class Demo {
public static void Main(String[] args) {
String str1 = "Brad";
String str2 = "Pitt";
Console.WriteLine("Strings before swap:");
Console.WriteLine("str1 = " + str1);
Console.WriteLine("str2 = " + str2);
// Step 1: Concatenate both strings
str1 = str1 + str2;
// Step 2: Extract original str1 and assign to str2
str2 = str1.Substring(0, str1.Length - str2.Length);
// Step 3: Extract original str2 and assign to str1
str1 = str1.Substring(str2.Length);
Console.WriteLine("\nStrings after swap:");
Console.WriteLine("str1 = " + str1);
Console.WriteLine("str2 = " + str2);
}
}
The output of the above code is −
Strings before swap: str1 = Brad str2 = Pitt Strings after swap: str1 = Pitt str2 = Brad
How It Works
The algorithm works by leveraging string concatenation and substring extraction −
-
Initial state: str1 = "Brad", str2 = "Pitt"
-
After concatenation: str1 = "BradPitt", str2 = "Pitt"
-
Extract original str1: str2 = str1.Substring(0, 8-4) = "Brad"
-
Extract original str2: str1 = str1.Substring(4) = "Pitt"
Alternative Approach Using XOR
For demonstration purposes, here's another approach using XOR operations on string character arrays −
using System;
class XORSwap {
public static void Main(String[] args) {
String str1 = "Hello";
String str2 = "World";
Console.WriteLine("Before swap: str1 = " + str1 + ", str2 = " + str2);
// Convert strings to character arrays for XOR swapping
if (str1.Length == str2.Length) {
char[] arr1 = str1.ToCharArray();
char[] arr2 = str2.ToCharArray();
for (int i = 0; i < arr1.Length; i++) {
arr1[i] = (char)(arr1[i] ^ arr2[i]);
arr2[i] = (char)(arr1[i] ^ arr2[i]);
arr1[i] = (char)(arr1[i] ^ arr2[i]);
}
str1 = new String(arr1);
str2 = new String(arr2);
}
Console.WriteLine("After swap: str1 = " + str1 + ", str2 = " + str2);
}
}
The output of the above code is −
Before swap: str1 = Hello, str2 = World After swap: str1 = World, str2 = Hello
Conclusion
Swapping strings without a temporary variable can be achieved using string concatenation and substring operations. The concatenation method is simple and works with strings of any length, while XOR-based approaches require equal-length strings and work at the character level.
