Code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) { String str, old, nword; Scanner sc = new Scanner(System.in); System.out.print("Enter a string: "); str = sc.nextLine(); System.out.print("Enter word you want to replace: "); old = sc.nextLine(); System.out.print("Enter new word: "); nword = sc.nextLine();
str = str.replace(old, nword);
System.out.println("\nNew string is: "+str); }
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String str, old, nword;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
str = sc.nextLine();
System.out.print("Enter word you want to replace: ");
old = sc.nextLine();
System.out.print("Enter new word: ");
nword = sc.nextLine();
str = str.replace(old, nword);
System.out.println("\nNew string is: "+str);
}
}
Output:
Tags:
Java Programs