+ 3

Can somebody help me with this stringindexoutofboundexception in this code?

I want to do find and replace word in a sentence without using replace method but I am stuck with this exception can somebody help me with this https://code.sololearn.com/cc3xpXNWWIai/?ref=app

5th May 2019, 12:12 PM
viki
viki - avatar
3 Réponses
5th May 2019, 1:22 PM
Denise Roßberg
Denise Roßberg - avatar
+ 3
Ya,it works. 😁😁tq buddy.
5th May 2019, 1:24 PM
viki
viki - avatar
+ 2
Your program works fine if the words have the same length. :) I think you need to calculate the difference between find.length() and replace.length() int a = str.length(); int b = find.length(); int c = replace.length(); --> a - b - c Or: int diff = (int)Math.abs(find.length() - replace.length()) for(int i = 0;i < a - diff;i++) { Math.abs() --> absolute value --> so you don't have to care about if you replace a smaller or larger wors then your given word.
5th May 2019, 12:32 PM
Denise Roßberg
Denise Roßberg - avatar