Given two string variables a and b, swap these variables without using temporary or third variable in Java. Use of library methods is allowed.
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 function 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.
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());
Strings before swap: a = Hello and b = World Strings after swap: a = World and b = Hello
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 email@example.com. 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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- User-defined Custom Exception in Java
- Using TreeMap to sort User-defined Objects in Java
- How to create a Java HashMap of user defined class type?
- How to create a user defined javap tool?
- Using _ (underscore) as variable name in Java
- Variable Arguments (Varargs) in Java
- Instance Variable Hiding in Java
- Instance variable as final in Java
- Final static variable in Java
- How to read a Matrix from user in Java?
- How to get the value of System Property and Environment Variable in Java?
- Local Variable Type Inference or LVTI in Java 10
- What is the difference between field, variable, attribute, and property in Java
- Initialization of local variable in a conditional block in Java
- How to swap or exchange objects in Java?
- Collections swap() method in Java with Examples
- Swap two variables in one line in C/C++, Python, PHP and Java
- Java program to swap first and last characters of words in a sentence
- Swapping items of a list in Java : Collections.swap() with Example
- Why non-static variable cannot be referenced from a static method in Java
Improved By : Akanksha_Rai