write a program in java to swap to string without using third variable
Answers
Answer:
public class SwapWithoutTemp {
public static void main(String args[]) {
String a = "hi";
String b = "hello";
System.out.println("Before swap: " + a + " " + b);
a = a + b;
b = a.substring(0, a.length() - b.length());
a = a.substring(b.length());
System.out.println("After : " + a + " " + b);
}
}
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
System.out.println("Enter Number a:");
String a = sc.nextLine();
System.out.println("Enter Number b:");
String b = sc.nextLine();
a = a+b;
b = a-b;
a = a-b;
System.out.println("a = "+a+" b = "+b);
}
}