Given two string variables a and b, swap these variables without using temporary or third variable in Java. Use of library methods is allowed.
Examples:
Input: a = "Hello"
b = "World"
Output:
Strings before swap: a = Hello and b = World
Strings after swap: a = World and b = Hello
The idea is to string concatenation and substring() method to perform this operation. The substring() method comes in two forms as listed below:
- substring(int beginindex): This fuction will return substring of the calling string starting from the index passed as argument to this function till the last character in the calling string.
- substring(int beginindex,int endindex): This function will return Substring of the calling string starting from the beginindex(inclusive) and ending at the endindex(exclusive) passed as argument to this function.
Algorithm:
1) Append second string to first string and
store in first string:
a = a + b
2) call the method substring(int beginindex, int endindex)
by passing beginindex as 0 and endindex as,
a.length() - b.length():
b = substring(0,a.length()-b.length());
3) call the method substring(int beginindex) by passing
b.length() as argument to store the value of initial
b string in a
a = substring(b.length());
// Java program to swap two strings without using a temporary // variable. import java.util.*; class Swap { public static void main(String args[]) { // Declare two strings String a = "Hello"; String b = "World"; // Print String before swapping System.out.println("Strings before swap: a = " + a + " and b = "+b); // append 2nd string to 1st a = a + b; // store intial string a in string b b = a.substring(0,a.length()-b.length()); // store initial string b in string a a = a.substring(b.length()); // print String after swapping System.out.println("Strings after swap: a = " + a + " and b = " + b); } } |
Output:
Strings before swap: a = Hello and b = World Strings after swap: a = World and b = Hello
References:
http://docs.oracle.com/
This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Remove Leading Zeros From String in Java
- Counting number of lines, words, characters and paragraphs in a text file using Java
- Reverse a string in Java
- Searching characters and substring in a String in Java
- Arrays in Java
- StringTokenizer class in Java with example | Set 1 ( Constructors)
- Split() String method in Java with examples
- Image Processing in Java | Set 1 (Read and Write)
- String to Integer in Java - parseInt()
- How to Initialize and Compare Strings in Java?
- Command Line arguments in Java
- Different ways for Integer to String Conversions In Java
- String class in Java | Set 1
- Scanner Class in Java
- String vs StringBuilder vs StringBuffer in Java


