import java.io.*; public class Replace{ public static void main(String[]args) throws IOException { InputStreamReader z= new InputStreamReader(System.in); BufferedReader br=new BufferedReader(z); System.out.println("Enter A String:"); String s=br.readLine(); System.out.println("Enter First Word:"); String w1=br.readLine(); System.out.println("Enter Second Word:"); String w2=br.readLine(); String rep=s.replace(w1,w2); System.out.println("Replaced String: "+rep); } }
Respected sir, extremely thanks to you for give me answer of my question but sir above code has a one problem when i'm input this string "This is a flower" and first word is "is" or second word is "was" then output is "Thwaswas a flower" and i want output like this e.g input string "This is a flower" first word "is " second word "was" then output will be print like this "This was a flower" not like this "Thwaswas a flower"
output after compiling your code
Enter A String: THIS IS A FLOWER Enter First Word: IS Enter Second Word: WAS Replaced String: THWASWASA FLOWER
Ads