Q:

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

belongs to collection: Java String Solved Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java String Solved Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a Java Program to Delete or Remove Vowels fr... >>
<< Write a Java Program to Swap Strings using temp va...