Write a Java Program to Swap two strings without using temp variable

By | 03.03.2017

Java Program to Swap two strings


To swap two string variables without using third or temp variable, we use substring() method  or replace() method of String class.

substring() method has two overloaded forms :-

1) substring(int beginIndex)

It returns substring of a calling string starting with the character at the specified index and ending with the last character of the calling string.

2) substring(int beginIndex, int endIndex)

It returns substring of a calling string starting with the character at beginIndex (inclusive) and ending with the character at endIndex (exclusive).

We use both of these forms to swap two string variables without using third variable.


Java Program to Swap Two Strings using replace() method


SOURCE CODE  1: :

 

import java.util.Scanner;
 
public class StringSwap 
{
        public static void main (String[] args) throws java.lang.Exception
        {
                Scanner sc = new Scanner(System.in);

                System.out.print("Enter First String : ");

                String str1 = sc.nextLine();

                System.out.print("Enter Second String : ");

                String str2 = sc.nextLine();

                System.out.println("\nBefore Swapping :");

                System.out.println("Str1 : "+str1);

                System.out.println("Str2 : "+str2);
            
                str1=str2+str1;
                str2=str1.replace(str2,"");
                str1=str1.replace(str2,"");
                
                System.out.println("\nAfter Swapping :");
         
                System.out.println("Str1 : "+str1);

                System.out.println("Str2 : "+str2);
        }
}

 

OUTPUT : :

 

Enter First String : Codez
Enter Second String : Club

Before Swapping :
Str1 : Codez
Str2 : Club

After Swapping :
Str1 : Club
Str2 : Codez

 


Java Program to Swap Two Strings using substring() method


SOURCE CODE 2 : :

 

import java.util.Scanner;
 
public class Swap_Strings 
{  
    public static void main(String[] args) 
    {    
        Scanner sc = new Scanner(System.in);
         
        System.out.print("Enter First String : ");
         
        String str1 = sc.nextLine();
         
        System.out.print("Enter Second String : ");
         
        String str2 = sc.nextLine();
         
        System.out.println("\nBefore Swapping :");
         
        System.out.println("Str1 : "+str1);
         
        System.out.println("Str2 : "+str2);
         
         
        str1 = str1 + str2;
         
        str2 = str1.substring(0, str1.length()-str2.length());
         
        str1 = str1.substring(str2.length());
         
         
        System.out.println("\nAfter Swapping :");
         
        System.out.println("Str1 : "+str1);
         
        System.out.println("Str2 : "+str2);
    }    
}

 

OUTPUT : :

 

Enter First String : ABCD
Enter Second String : abcd

Before Swapping :
Str1 : ABCD
Str2 : abcd

After Swapping :
Str1 : abcd
Str2 : ABCD
5 1 vote
Article Rating
Category: Java Programming String Programs Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments