
import java.io.*; public class Replace { public void main()throwsIoException { InputStreamReader = new InputStreamReader(System.in); BufferReader br=new BufferReader(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 str=" "; String st=" "; for(int i=0;i<=s.lenth();i++); { char ch=s.charAt(i); if(ch!=' ') { str=str+ch; { if(str.equals(w1)) { st=s.replace(w1,w2); } System.out.println(st); } } } output of above code Enter A String This is a flower Enter First Word is Enter Second Word was after replace string will be This was a flower note:- The word 'is' is existing in string so it's replace with second word 'was'.
Respected sir i've a problem with above code i unable to understand that what's the problem in above code because output is not so rightly so plz help me and follow these instruction 1 do not use Stringtokenizer 2 do not use reverse method 3 do not use StringBufferReader 4 do not use Split method

Java replace string
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);
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.