If we want to swap two Strings without using temporary variable,hence we can serve this purpose using this program.
lets take example:- String1-Noteshacker, String2- Website
After swapping it become: Website Noteshacker.
Explanation:
1. First we take two strings (s1=”Noteshacker”,s2=”Website”) as an input then we add/concatenate both of them and save it to String variable s1.
2. Now length of s1 is sum of s1+s2.i.e. s1=NoteshackerWebsite.i.e. length=17
3. Perform s1.substring(0, (s1.length() – s2.length()))and save it to string variable s2 by this we extracted Notehacker and save it to s2
4. Perform s1.substring(s2.length()) by this Website is extracted and save it to s1
5. Now s1=Website and s2=Noteshacker .
6. Simply print them s1 and s2,hence swapping done.
Algorithm/Explanation
- Start
- First take two String Input s1 and s2
- s1=Noteshacker s2=Website
- you can take user defined or static input
- Print original string Noteshacker Website
- Perform s1=s1.concat(s2). // s1=NoteshackerWebsite also length og s1=17
- Perform s2= s1.substring(0, (s1.length() – s2.length())); // 17-7 =10 ” Noteshacker “is extrated
- Performs1=s1=s1.substring(s2.length());// “Website “is extracted
- Print s1 and s2
- End
Program
/*
Java Program to Swap two String Without using temporary variable */
import java.util.Scanner;
public class SwappingString {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter String-1");
String s1 = sc.nextLine();
System.out.println("Enter String-2");
String s2 = sc.nextLine();
System.out.println("String Before Swapping:" + s1 + " " + s2);
s1 = s1.concat(s2); //length=17
s2 = s1.substring(0, (s1.length() - s2.length())); // 17-7 =10 noteshacker
s1=s1.substring(s2.length());// Website
System.out.println("String Before Swapping:" + s1 + " " + s2);
}
}
Output:
Enter String-1
Noteshacker
Enter String-2
Website
String Before Swapping: Noteshacker Website
String Before Swapping: Website Noteshacker
Related Topics
- Arithmetic Operations Java Program
- Java program to perform Logical and Relational operations
- Java program to Implement Mathematical functions defined in Java Math class
- Java basic Programs with Explanation
- Java Program to find Min,Max Element in an Array
- Java Program to sort Array Elements in ascending and descending order
- Java Program to display Array item at Even & Odd position
- Program to Calculate Sum and Average of Array elements
- Java Program to find Index of specific Element in an array
- Program to find common elements between two arrays
- Java Program to Add/Insert specific Element to an Array
- Java Program to delete specific element from an Array
- Program to calculate frequency of each element in Array
- Java Program to find Even and Odd Numbers in the arrays
- Java Program to search Given Number in an Array
- Java Program to Add or Sub Two Matrices
- Java Program to Multiply two or More Matrices
- Java Program to Print Array in Reverse Order
- Java Program To Transpose Given Matrix
- Java Programs List- Java Programs Examples With Output