codescracker


java

Java Program to Swap Two Strings



« Previous Program Next Program »


Swap Two Strings

To swap two string in Java Programming, you have to first ask to the user to enter the two string, then make a temp variable of the same type. Now place the first string in the temp variable, then place the second string in the first, now place the temp string in the second.

Java Programming Code to Swap Two Strings

Following Java Program ask to the user to enter the two strings (you can enter the two name or two word), to swap the strings, then display the result after swapping the strings:

/* Java Program Example - Swap Two Strings */
		
import java.util.Scanner;

public class JavaProgram
{
    public static void main(String[] input)
    {
        String str1, str2, strtemp;
        Scanner scan = new Scanner(System.in);
        
        System.out.print("Enter First String : ");
        str1 = scan.nextLine();
        System.out.print("Enter Second String : ");
        str2 = scan.nextLine();
        
        System.out.println("\nStrings before Swapping are :");
        System.out.print("String 1 = " +str1+ "\n");
        System.out.print("String 2 = " +str2+ "\n");
        
        strtemp = str1;
        str1 = str2;
        str2 = strtemp;
        
        System.out.println("\nStrings after Swapping are :");
        System.out.print("String 1 = " +str1+ "\n");
        System.out.print("String 2 = " +str2+ "\n");
    }
}

When the above Java Program is compile and executed, it will produce the following output:

Java Program swap two strings

Same Program in Other Languages

You may also like to learn and practice the same program in other popular programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test