Write a program to enter two strings s1 and s2 , and swap them. Print the strings before and after swap.
THE FOLLOWING PROGRAM SEGMENT SHOWS HOW TO ENTER TWO STRINGS AND SWAP THEM .
import java.util.*;
class swapSTRINGS
{
public static void main (String args[])
{
Scanner ob = new Scanner (System.in);
System.out.println("Enter 2 strings");
String s1 = ob.nextLine();
String s2 = ob.nextLine();
System.out.println("The strings before interchange are ");
System.out.println("The String s1 = "+s1);
System.out.println("The String s2 = "+s2);
String temp = "";
temp = s1;
s1 = s2;
s2 = temp;
System.out.println("The strings after interchange are = ");
System.out.println("The String s1 ="+s1);
System.out.println("The String s2 = "+s2);
}
}
INPUT:
Enter 2 strings
askris
blogger
OUTPUT:
The strings before interchange =
The String s1= askris
The String s2 = blooger
The strings after interchange =
The String s1 = blooger
The String s2 =askris
The above program CODE is compiled and is without any error.
Hope you guys found it helpful.
Please share it with your friends and comment down in the comment section below.
Follow https://askrisfor.blogspot.com/ for more such updates.
Visit us again.
Thank you!!
Comments
Post a Comment