Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

notReplace challenge

Hi,

I am working on below challenge

http://codingbat.com/prob/p154137

I am not clear on challenge desciption and  how to approach. please advise
Avatar of zzynx
zzynx
Flag of Belgium image

We won't (and are not allowed to) write the code for you.
At least come up with some code you wrote yourself.
Starting from that we can (try to) help you further.
All these questions should be deleted.
You have posted EIGHT questions in SEVEN minutes.  Very impressive.
But you are obviously not giving these question any thought or effort of you own.

And you posted four more yesterday.  What are you trying to accomplish?  It certainly isn't learning.

If you ask for advice before doing any thinking, and then post shoddy code without trying to debug it yourself, and then move on to the next question without any introspection or review, then you have actually managed to avoid learning anything.

The CodingBat questions are Challenges, not because they are difficult, but because they are subtle.
You can race through them, or you can spend some time finding something closer to the optimum, efficient, elegant solution.  You can spend your time any way you want, but please stop wasting ours.
Avatar of gudii9

ASKER

Given a string, return a string where every appearance of the lowercase word "is" has been replaced with "is not". The word "is" should not be immediately preceeded or followed by a letter -- so for example the "is" in "this" does not count. (Note: Character.isLetter(char) tests if a char is a letter.)

notReplace("is test") → "is not test"
notReplace("is-is") → "is not-is not"
notReplace("This is right") → "This is not right"

is should be replaced with is not? let my try using eclipse. without using eclipse it is hard to solve these 3rd level complex string challenges to me
Avatar of gudii9

ASKER

public class NotReplace   {
/*
Given a string, return a string where every appearance of the lowercase word "is" has been replaced with "is not". The word "is" should not be immediately preceeded or followed by a letter -- so for example the "is" in "this" does not count. (Note: Character.isLetter(char) tests if a char is a letter.)

notReplace("is test") → "is not test"
notReplace("is-is") → "is not-is not"
notReplace("This is right") → "This is not right"*/
	
	public static void main(String[] args) {
		System.out.println("value-->"+notReplace("is test"));
		
	}
	
	public static String notReplace(String str ) {
	String str2=str.replace("is", "is not");
		return str2;
		  
	}

}

Open in new window


i wrote as above. got below output

value-->is not test


seems working fine. let me test other tests
Avatar of gudii9

ASKER

public String notReplace(String str) {
  String str2=str.replace("is", "is not");
            return str2;
             
}

i am failing some tests as below. please advise
Expected      Run            
notReplace("is test") → "is not test"      "is not test"      OK      
notReplace("is-is") → "is not-is not"      "is not-is not"      OK      
notReplace("This is right") → "This is not right"      "This not is not right"      X      
notReplace("This is isabell") → "This is not isabell"      "This not is not is notabell"      X      
notReplace("") → ""      ""      OK      
notReplace("is") → "is not"      "is not"      OK      
notReplace("isis") → "isis"      "is notis not"      X      
notReplace("Dis is bliss is") → "Dis is not bliss is not"      "Dis not is not blis nots is not"      X      
notReplace("is his") → "is not his"      "is not his not"      X      
notReplace("xis yis") → "xis yis"      "xis not yis not"      X      
notReplace("AAAis is") → "AAAis is not"      "AAAis not is not"      X      
other tests
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

The word "is" should not be immediately preceeded or followed by a letter -- so for example the "is" in "this" does not count

i got the meaning. let me think code
First tell us in words how you think this should work
Avatar of gudii9

ASKER

sure
Avatar of gudii9

ASKER

public String notReplace(String str) {
  String str2="";
  for(int i=0;i<=str.length()-2;i++)
  if((str.substring(i,i+2)).equals("is")){
  str2=str.replace("is", "is not");
  }
		return str2;
		  
}

Open in new window


i tried and succeded in few and failed in few
Expected	Run		
notReplace("is test") → "is not test"	"is not test"	OK	
notReplace("is-is") → "is not-is not"	"is not-is not"	OK	
notReplace("This is right") → "This is not right"	"This not is not right"	X	
notReplace("This is isabell") → "This is not isabell"	"This not is not is notabell"	X	
notReplace("") → ""	""	OK	
notReplace("is") → "is not"	"is not"	OK	
notReplace("isis") → "isis"	"is notis not"	X	
notReplace("Dis is bliss is") → "Dis is not bliss is not"	"Dis not is not blis nots is not"	X	
notReplace("is his") → "is not his"	"is not his not"	X	
notReplace("xis yis") → "xis yis"	"xis not yis not"	X	
notReplace("AAAis is") → "AAAis is not"	"AAAis not is not"	X	
other tests

Open in new window

please advise
Avatar of gudii9

ASKER

public String notReplace(String str) {
  String str2="";
  for(int i=0;i<=str.length()-2;i++)
  if( (str.substring(i-1,i)).length()==0 && (str.substring(i,i+2).equals("is") )){
  str2=str.replace("is", "is not");
  }
            return str2;
             
}
tried checking one character before 'is' a blank space but faiing below test
Expected	Run		
notReplace("is test") → "is not test"	Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:4)	X	
notReplace("is-is") → "is not-is not"	Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:4)	X	
notReplace("This is right") → "This is not right"	Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:4)	X	
notReplace("This is isabell") → "This is not isabell"	Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:4)	X	
notReplace("") → ""	""	OK	
notReplace("is") → "is not"	Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:4)	X	
notReplace("isis") → "isis"	Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:4)	X	
notReplace("Dis is bliss is") → "Dis is not bliss is not"	Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:4)	X	
notReplace("is his") → "is not his"	Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:4)	X	
notReplace("xis yis") → "xis yis"	Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:4)	X	
notReplace("AAAis is") → "AAAis is not"	Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:4)	X	
other tests
X	
Your progress graph for this problem


Forget It! -- delete my code for this problem

Open in new window

Avatar of gudii9

ASKER

public String notReplace(String str) {
  String str2="";
  for(int i=0;i<=str.length()-2;i++)
  if( str.substring(i-1,i).equals(" ") && (str.substring(i,i+2).equals("is") )){
  str2=str.replace("is", "is not");
  }
		return str2;
		  
}

Open in new window


above also failing same tests
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>  please advise

I advise going back to your oldest questions, and working to close them one at a time.
You can ask to delete any questions that are poorly asked or hopelessly confused.
First tell us in words how you think this should work
Sure
And what do you post in the next comment? CODE.
Avatar of gudii9

ASKER

Given a string, return a string where every appearance of the lowercase word "is" has been replaced with "is not". The word "is" should not be immediately preceeded or followed by a letter -- so for example the "is" in "this" does not count. (Note: Character.isLetter(char) tests if a char is a letter.)

notReplace("is test") → "is not test"
notReplace("is-is") → "is not-is not"
notReplace("This is right") → "This is not right"


step 1:  identify string str
step2: find if it has "is"
step3: if yes replace "is" with "is not"
step4:if no ignore
step5:return step3 modified string

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NotReplace {
	public static void main(String[] args) {

		System.out.println("value is--->" + notReplace("is test"));
	}

	public static String notReplace(String str) {
		/*
		 * String str2 = ""; for (int i = 0; i <= str.length() - 2; i++) if
		 * (str.substring(i - 1, i).equals(" ") && (str.substring(i, i +
		 * 2).equals("is"))) { str2 = str.replace("is", "is not"); }
		 */
		String REGEX = "is";
		String INPUT = str;
		String REPLACE = "is not";
		// public static void main(String[] args) {
		Pattern p = Pattern.compile(REGEX);
		// get a matcher object
		Matcher m = p.matcher(INPUT);
		StringBuffer sb = new StringBuffer();
		while (m.find()) {
			m.appendReplacement(sb, REPLACE);
		}
		m.appendTail(sb);
		System.out.println(sb.toString());

		return sb.toString();

	}

}

Open in new window


i got correct output like
is not test
value is--->is not test

but some tests failing where there are letters before "this"

how to fix. please advise
Re-read the Challenge (especially the Note) and all of the earlier posts, and then try Step 3 again.
It's clear that your step 3
step3: if yes replace "is" with "is not"
is absolutely not in line with
The word "is" should not be immediately preceeded or followed by a letter
So you have to rethink your way of working
Avatar of gudii9

ASKER

public String notReplace(String str) { String result = "";
    str = " " + str + "  "; 
    for (int i = 0; i < str.length() - 2; i++) {
        if (str.charAt(i) == 'i') {
            if (str.charAt(i + 1) == 's'
                    && !Character.isLetter(str.charAt(i + 2))
                    && !Character.isLetter(str.charAt(i - 1))) {
                result += "is not";
                i += 1;
            } else result += "i";
        } else result += str.charAt(i);
    }
    return result.substring(1);
}

Open in new window


above code is working fine

I was not ler why we are appending empty space before and after as below?
str = " " + str + "  "; 

Open in new window

I am not ler either.
why we are appending empty space before and after
After all those string manipulating challenges you have already solved, I would expect you to know.
Remove those empty strings and see what happens.
It's the best way to learn: experiment with the code.

If you don't add those spaces, I bet you will have StringIndexOutOfBoundsException all over the place.
After all those String manipulating challenges, I expect you to know why.
If you don't know, I seriously doubt if you learn anything by "solving" this challenges the way you currently do...
Avatar of gudii9

ASKER

 str = " " + str + "  "; 

Open in new window


 str = + str + ;
i see if i remove empty strings as above all tests failing

 str = "" + str + " ";

i see if i remove one empty space also as above all tests failing

 str = "" + str + "";
i see if i remove all empty spaces also as above all tests failing

i wonder how you got idea to exactly give one empty space before str and two empty spaces after to pass all tests?
please advise
... running away screaming . . . !
Why are you using spaces at all?  There is no mention of spaces in the challenge.  There is a valuable hint though.
» i wonder how you got idea to exactly give one empty space before str and
    two empty spaces after . . .

I think you came up with this terrible idea all on your own.
Avatar of gudii9

ASKER

I aee isChar method gint. I need empty apace before and after is to replace it with is not
Let me think
Read the Challenge.  Tell me where you see "space" or "blank"?
What does the Challenge actually say?

Try this test string:
Tell me what the answer is.
See the problem with your approach yet?
Avatar of gudii9

ASKER

Given a string, return a string where every appearance of the lowercase word "is" has been replaced with "is not". The word "is" should not be immediately preceeded or followed by a letter -- so for example the "is" in "this" does not count. (Note: Character.isLetter(char) tests if a char is a letter.)

notReplace("is test") → "is not test"
notReplace("is-is") → "is not-is not"
notReplace("This is right") → "This is not right"

Open in new window

it says
Avatar of gudii9

ASKER

I think you came up with this terrible idea all on your own.

how to get good idea and approach, design from possible many approaches? please advise
Read the Challenge.  Tell me where you see "space" or "blank"?
ANSWER THE QUESTION

What does the Challenge actually say?
Why haven't you used the GIANT HINT?
Avatar of gudii9

ASKER

which hint you are talking about?
you mean below

Note: Character.isLetter(char) tests if a char is a letter.)


i have already used Character.isLetter(char) though

public String notReplace(String str) { String result = "";
    str = " " + str + "  "; 
    for (int i = 0; i < str.length() - 2; i++) {
        if (str.charAt(i) == 'i') {
            if (str.charAt(i + 1) == 's'
                 [b]   && !Character.isLetter(str.charAt(i + 2))
                    && !Character.isLetter(str.charAt(i - 1))) {[/b]
                result += "is not";
                i += 1;
            } else result += "i";
        } else result += str.charAt(i);
    }
    return result.substring(1);
}

Open in new window

Avatar of gudii9

ASKER

Read the Challenge.  Tell me where you see "space" or "blank"?
ANSWER THE QUESTION

challenge did not mention about space or blank at all.

all it said


Given a string, return a string where every appearance of the lowercase word "is" has been replaced with "is not". The word "is" should not be immediately preceeded or followed by a letter -- so for example the "is" in "this" does not count. (Note: Character.isLetter(char) tests if a char is a letter.)

notReplace("is test") → "is not test"
notReplace("is-is") → "is not-is not"
notReplace("This is right") → "This is not right"
Finally . . .  

The word "is" should not be immediately preceeded or followed by a letter

And the Note tells you what function to use.  I advise you to use it.
If you come here @ EE to learn from experts, then you should listen carefully what they advise you AND DO IT.
If we ask you to first think about how to do it, then write it in words and only after that, write code, you'd better follow that advice. It's not to frustrate or irritate you, it's to help you.
You are too much focused on the Java language instead of learning to think like a coder.
You have to learn to think as a coder FIRST. The language in which you write your thoughts afterwards is of lesser importance. That's why I insist that you should first write the process in words (kind of so called "pseudo code"), but all you do is posting Java code and more java code...
>>  i have already used Character.isLetter(char) though

What were your results and conclusions when you ran the test strings?
Avatar of gudii9

ASKER

public String notReplace(String str) { String result = "";
    str = " " + str + "  ";
    for (int i = 0; i < str.length() - 2; i++) {
        if (str.charAt(i) == 'i') {
            if (str.charAt(i + 1) == 's'
                   && !Character.isLetter(str.charAt(i + 2))
                    && !Character.isLetter(str.charAt(i - 1))) {

                result += "is not";
                i += 1;
            } else result += "i";
        } else result += str.charAt(i);
    }
    return result.substring(1);
}

as above i  used !Character.isLetter(). Result is all tests i passed with above terrible approach of appending and prepending some blank spaces.
If we ask you to first think about how to do it, then write it in words and only after that, write code, you'd better follow that advice. It's not to frustrate or irritate you, it's to help you.
You are too much focused on the Java language instead of learning to think like a coder.
You have to learn to think as a coder FIRST. The language in which you write your thoughts afterwards is of lesser importance. That's why I insist that you should first write the process in words (kind of so called "pseudo code"), but all you do is posting Java code and more java code...
let me write psuedo code for this challenge
Avatar of gudii9

ASKER


Given a string, return a string where every appearance of the lowercase word "is" has been replaced with "is not". The word "is" should not be immediately preceeded or followed by a letter -- so for example the "is" in "this" does not count. (Note: Character.isLetter(char) tests if a char is a letter.)

psedo code steps:

1.Given string str check all possible apperarances of is by looping over string
2. make sure "is" is is not immediately preceeded or followed by a letter(which means special character or empty blanks are ok)
3. if it satisfyies above condition increment count by 1
4.if it does not satisfy condition in step 2 count remains same.
5. continue loop till end by handling edge conditions
6. return count as output
Concerning your pseudo code:
* It doesn't do any replacement of 'is' by 'not is', so it can't be correct.
* step 6 looks incorrect to me since you are asked to return a modified string (What's your intention with that count?)
* Try to be more specific about the edge conditions

PS. No time to write any code yet!  The pseudo code doesn't feel correct at this stage.
Avatar of gudii9

ASKER

It doesn't do any replacement of 'is' by 'not is', so it can't be correct.
* step 6 looks incorrect to me since you are asked to return a modified string (What's your intention with that count?)
* Try to be more specific about the edge conditions

sure let me fix
Avatar of gudii9

ASKER

1.Given string str check all possible apperarances of is by looping over string
2. make sure "is" is is not immediately preceeded or followed by a letter(which means special character or empty blanks are ok)
3. if it satisfies above condition replace" is " with "is not"
4.if it does not satisfy condition in step 2 return "is" as "is".
5. continue loop till end.
6.  handle edge conditions properly to make sure strl.length<=str.length() in for loop
7. return str as output
Avatar of gudii9

ASKER

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NotReplace {
	public static void main(String[] args) {

		// System.out.println("is--->" + notReplace("is test"));
		System.out.println(" is--->" + notReplace("is test"));
		/*System.out.println(" is--->" + notReplace("is-is"));
		System.out.println(" is--->" + notReplace("This is right"));
		System.out.println("is--->" + notReplace("This is isabell"));
		System.out.println(" is--->" + notReplace(""));
		System.out.println(" is--->" + notReplace("is"));
		System.out.println(" is--->" + notReplace("isis"));
		System.out.println("is--->" + notReplace("Dis is bliss is"));
		System.out.println(" is--->" + notReplace("is his"));
		System.out.println(" is--->" + notReplace("xis yis"));
		System.out.println(" is--->" + notReplace("AAAis is"));
*/
	}

	public static String notReplace(String str) {
		/*
		 * String str2 = ""; for (int i = 0; i <= str.length() - 2; i++) if
		 * (str.substring(i - 1, i).equals(" ") && (str.substring(i, i +
		 * 2).equals("is"))) { str2 = str.replace("is", "is not"); }
		 */
		String result = "";
		String target="is";
		//str = " " + str + "  "; // avoid issues with corner cases
		for (int i = 0; i < str.length() - 2; i++) {/*
			if (str.charAt(i) == 'i') {
				if (str.charAt(i + 1) == 's' && !Character.isLetter(str.charAt(i + 2))
						&& !Character.isLetter(str.charAt(i - 1))) {
					result += "is not";
					i += 1;
				} else
					result += "i";
			} else
				result += str.charAt(i);
		*/
			if(str.toLowerCase().contains(target.toLowerCase())	){
				str=str.replace("is", "is not");
				System.out.println("str is in if loop-->"+str);
				
			}
			else{
				str=str;
				
			}
			//return str;
		}
		return str;
	}

}

Open in new window


modified as above and going in infinity loop
Avatar of gudii9

ASKER

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NotReplace {
	public static void main(String[] args) {

		// System.out.println("is--->" + notReplace("is test"));
		System.out.println(" is--->" + notReplace("is test"));

		
		System.out.println(" is--->" + notReplace("This is right"));
		/*System.out.println("is--->" + notReplace("This is isabell"));
		System.out.println(" is--->" + notReplace(""));
		System.out.println(" is--->" + notReplace("is"));
		System.out.println(" is--->" + notReplace("isis"));
		System.out.println("is--->" + notReplace("Dis is bliss is"));
		System.out.println(" is--->" + notReplace("is his"));
		System.out.println(" is--->" + notReplace("xis yis"));*/
		//System.out.println(" is--->" + notReplace("AAAis is"));
		//System.out.println(" is--->" + notReplace("is-is"));
	}

	public static String notReplace(String str) {
		/*
		 * String str2 = ""; for (int i = 0; i <= str.length() - 2; i++) if
		 * (str.substring(i - 1, i).equals(" ") && (str.substring(i, i +
		 * 2).equals("is"))) { str2 = str.replace("is", "is not"); }
		 */
		String result = "";
		String target = "is";
		String target2 = "is not";
		// str = " " + str + " "; // avoid issues with corner cases
		for (int i = 0; i < str.length()- 2; i++) {/*
							 * if (str.charAt(i) == 'i') { if (str.charAt(i + 1)
							 * == 's' && !Character.isLetter(str.charAt(i + 2))
							 * && !Character.isLetter(str.charAt(i - 1))) {
							 * result += "is not"; i += 1; } else result += "i";
							 * } else result += str.charAt(i);
							 */
			if ((str.substring(i, i + 2)).toLowerCase().contains(target.toLowerCase())) {
				str = str.replace(target, target2);
				System.out.println("str is in if loop-->" + str);
				i++;

			} else {
				str = str;
				System.out.println("in else...");

			}
			// return str;
		}
		return str;
	}

}

Open in new window


above code works fine with below

System.out.println(" is--->" + notReplace("is test"));


with below it goes to infinite loop some reason.

System.out.println(" is--->" + notReplace("This is right"));

please advise
Avatar of gudii9

ASKER

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NotReplace {
	public static void main(String[] args) {

		// System.out.println("is--->" + notReplace("is test"));
		//System.out.println(" is--->" + notReplace("is test"));

		
		System.out.println(" is--->" + notReplace("This is right"));
		/*System.out.println("is--->" + notReplace("This is isabell"));
		System.out.println(" is--->" + notReplace(""));
		System.out.println(" is--->" + notReplace("is"));
		System.out.println(" is--->" + notReplace("isis"));
		System.out.println("is--->" + notReplace("Dis is bliss is"));
		System.out.println(" is--->" + notReplace("is his"));
		System.out.println(" is--->" + notReplace("xis yis"));*/
		//System.out.println(" is--->" + notReplace("AAAis is"));
		//System.out.println(" is--->" + notReplace("is-is"));
	}

	public static String notReplace(String str) {
		/*
		 * String str2 = ""; for (int i = 0; i <= str.length() - 2; i++) if
		 * (str.substring(i - 1, i).equals(" ") && (str.substring(i, i +
		 * 2).equals("is"))) { str2 = str.replace("is", "is not"); }
		 */
		String result = "";
		String target = "is";
		String target2 = "is not";
		//String result;
		// str = " " + str + " "; // avoid issues with corner cases
		for (int i = 0; i < str.length()- 2; i++) {/*
							 * if (str.charAt(i) == 'i') { if (str.charAt(i + 1)
							 * == 's' && !Character.isLetter(str.charAt(i + 2))
							 * && !Character.isLetter(str.charAt(i - 1))) {
							 * result += "is not"; i += 1; } else result += "i";
							 * } else result += str.charAt(i);
							 */
			if ((str.substring(i, i + 2)).toLowerCase().contains(target.toLowerCase())) {
				result = str.replace(target, target2);
				System.out.println("str is in if loop-->" + str);
				i++;

			} else {
				result = str;
				System.out.println("in else...");

			}
			// return str;
		}
		return result;
	}

}

Open in new window


i took care of the infinite loop as i assigning to same str due to that its length keep on increasing.

Now i am returning result string instead. Still need to fix other things
If you use a method you have to read its documentation first to see if what it does is what you want.
You write
result = str.replace(target, target2);

Open in new window

The documentation says that it
Replaces EACH substring of this string that matches the literal target sequence with the specified literal replacement sequence.

That doesn't do what you wrote in step 3.
3. if it satisfies above condition replace" is " with "is not"
(Supposing that with "is", you meant the "is" that you just encountered)

When you write the way of working in pseudo code, you have to stick to that and make sure that the real code you write in Java does what you intend to make it do.
If you write your code, try to place your 7 steps as comments besides your java lines of code.
Also, use the method Character.isLetter(char)
Avatar of gudii9

ASKER

public String notReplace(String str) { String result = "";
    str = " " + str + "  "; 
    for (int i = 0; i < str.length() - 2; i++) {
        if (str.charAt(i) == 'i') {
            if (str.charAt(i + 1) == 's'
                    && !Character.isLetter(str.charAt(i + 2))
                    && !Character.isLetter(str.charAt(i - 1))) {
                result += "is not";
                i += 1;
            } else result += "i";
        } else result += str.charAt(i);
    }
    return result.substring(1);
}

Open in new window


above works fine. looks like substring  is terible approach for this challenge?
how to know which method  of string or approach as here checking str.charAt and then checking before and after !Character.isLetter() seems only possible solution?
Avatar of gudii9

ASKER

public String notReplace(String str) {
  String result = "";
  int len = str.length();
  
  for(int i = 0; i < len; i++){
    if(i-1 >= 0 && Character.isLetter(str.charAt(i-1))
    || i+2 < len && Character.isLetter(str.charAt(i+2))) {
      result += str.charAt(i);
    }
    else if(i+1 < len && str.substring(i, i+2).equals("is")) {
      result += "is not";
      i++;
    }
    else result += str.charAt(i);
  }
  return result;
}

Open in new window


above works fine
Avatar of gudii9

ASKER

i was not completely clear on above approach.

public String notReplace(String str) {
    String result = "";
    str = " " + str + "  "; // avoid issues with corner cases
    for (int i = 0; i < str.length() - 2; i++) {
        if (str.charAt(i) == 'i') {
            if (str.charAt(i + 1) == 's'
                    && !Character.isLetter(str.charAt(i + 2))
                    && !Character.isLetter(str.charAt(i - 1))) {
                result += "is not";
                i += 1;
            } else result += "i";
        } else result += str.charAt(i);
    }
    return result.substring(1);
}

Open in new window


above is clear.

i took pencil paper and a slate with marker went through each character of character of below string

This is right
Great you finally found it.
Here's a slightly  alternative approach that you may want to research/study:

public static String notReplace(String str) {
        str = " " + str + " ";
        String target = "is";
        String target2 = "is not";
        for (int i=1; i < str.length()-2; i++) { // 1.Given string str check all possible apperarances of is by looping over string

            if ( target.equals(str.substring(i, i+2)) &&
                    !Character.isLetter(str.charAt(i-1)) &&
                    !Character.isLetter(str.charAt(i+2)) ) { // 2. make sure "is" is is not immediately preceeded or followed by a letter(which means special character or empty blanks are ok)
                // 3. if it satisfies above condition replace "is" with "is not"
                str = str.substring(0, i) + target2 + str.substring(i+2);
            }
        }
        return str.substring(1, str.length()-1); // 7. return str as output
}

Open in new window

Avatar of gudii9

ASKER

without pen pensile these challenges are mountains
with pen pencil like molehills easy
Then use pen pencil first in the future
Avatar of gudii9

ASKER

exactly i will keep always one pencil paper and also a erasable slate and dry marker which i got from walmart for 2$.
Avatar of gudii9

ASKER

keeping in my mind about whole new challenge and flow and index positions etc all once is challenging. once i put it psedo code steps and use slate/board i am able to put index arrow at each letter and see what happens when i am in for loop there and what conditions there etc