public class Main { public static void main(String[] args) { String s1 = "One"; String s2 = "Two"; // Print String before swapping System.out.println("Strings before swap: s1 = " +s1 + " and s2 = "+s2); // append 2nd string to 1st s1 = s1 + s2; // store initial string a in string b s2 = s1.substring(0,s1.length()-s2.length()); // store initial string b in string a s1 = s1.substring(s2.length()); // print String after swapping System.out.println("Strings after swap: s1 = " + s1 + " and s2 = " + s2); } }
Strings before swap: s1 = One and s2 = Two
Strings after swap: s1 = Two and s2 = One