wordEnds java challenge

Hi,
I am working on below challenge
http://codingbat.com/prob/p147538
I tried my code as below
public String wordEnds(String str, String word) {
int xyPos=str.indexOf("xy");
String s1=str.substring(xyPos,xyPos-1);
String s2=str.replaceAll(str,word);
String s3=s1+s2;
return s3;
  
}

Open in new window

I am getting below result
Expected      Run            
wordEnds("abcXY123XYijk", "XY") → "c13i"      "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:3)"      X         
wordEnds("XY123XY", "XY") → "13"      "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:3)"      X         
wordEnds("XY1XY", "XY") → "11"      "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:3)"      X         
wordEnds("XYXY", "XY") → "XY"      "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:3)"      X         
wordEnds("XY", "XY") → ""      "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:3)"      X         
wordEnds("Hi", "XY") → ""      "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:3)"      X         
wordEnds("", "XY") → ""      "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:3)"      X         
wordEnds("abc1xyz1i1j", "1") → "cxziij"      "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:3)"      X         
wordEnds("abc1xyz1", "1") → "cxz"      "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:3)"      X         
wordEnds("abc1xyz11", "1") → "cxz11"      "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:3)"      X         
wordEnds("abc1xyz1abc", "abc") → "11"      "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:3)"      X         
wordEnds("abc1xyz1abc", "b") → "acac"      "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:3)"      X         
wordEnds("abc1abc1abc", "abc") → "1111"      "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:3)"      X         
other tests
X         
Your progress graph for this problem

how to  improve my approach, results and design of this challenge. How do i make a graphical venn or flow chart or some other relevant diagram to design it before writing code to decide best strategy?
 Please advise
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gheistCommented:
Improvement to approach would be to do yourself instead of asking others to pass your exam.
ozoCommented:
public String wordEnds(String str, String word) {
   return str.replaceAll("(?:(?!.?"+word+").)*(?:(.)?"+word+"(?=(.)"+word+")?"+"(?="+word+"(?<=(.)))?(.)?)?","$1$2$4$3");
}
// this could be easier using matcher.group() instead of replaceAll(), but I don't see a way to include java.util.regex.Matcher from codingbat
gudii9Author Commented:
i was not clear on challenge

wordEnds("abcXY123XYijk", "XY") → "c13i"//this is where c and i are coming?
wordEnds("XY123XY", "XY") → "13"//how 13 came?
wordEnds("XY1XY", "XY") → "11"//how 11 came?
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

ozoCommented:
wordEnds("abcXY123XYijk", "XY") → "c13i"//this is where c and i are coming?
this is where c and 1 are coming
i is coming from
wordEnds("abcXY123XYijk", "XY") → "c13i"

wordEnds("XY123XY", "XY") → "13"//how 13 came?
wordEnds("XY123XY", "XY") → "13"
wordEnds("XY123XY", "XY") → "13"

wordEnds("XY1XY", "XY") → "11"//how 11 came?
wordEnds("XY1XY", "XY") → "11"
wordEnds("XY1XY", "XY") → "11"
gudii9Author Commented:
wordEnds("abcXY123XYijk", "XY") → "c13i"//this is where c and i are coming?
this is where c and 1 are coming
i is coming from
wordEnds("abcXY123XYijk", "XY") → "c13i"

why 2 is filtered out ??
if more than one word( XY) say two word(XY) as above which word(XY) we have to consider first XY or second XY to identify

return a string made of each char just before and just after every appearance of the word in the string. I
gudii9Author Commented:
i think now i understand
we want each before each after characters for every occurence of word..
gudii9Author Commented:
public class WordEndsEx {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

       System.out.println("value is----"+wordEnds("abcXY123XYijk", "XY")); //"c13i"
	}

	
	public static String wordEnds(String str, String word) {
		int xyPos=str.indexOf("XY");
		int xyPosition=str.lastIndexOf("XY");
		System.out.println("xyPos"+xyPos);
		System.out.println("xyPosition"+xyPosition);
		String s1=str.substring(xyPos-1,xyPos);
		String s2=str.substring(xyPos+2,xyPos+3);
		//String s2=str.replaceAll(str,word);
		String s3=str.substring(xyPosition-1,xyPosition);
		String s4=str.substring(xyPosition+2,xyPosition+3);
		System.out.println("s1 is"+s1);
		System.out.println("s2 is"+s2);
		System.out.println("s3 is"+s3);
		System.out.println("s4 is"+s4);
		//String s4=str.replaceAll(str,word);
		//String s5=s1+s2;
		//String s6=s3+s4;
		String s7=s1+s2+s3+s4;
		return s7;
		  
		}
}

Open in new window


above gave below correct result.

xyPos3
xyPosition8
s1 isc
s2 is1
s3 is3
s4 isi
value is----c13i


how to improve my code above?
please advise
gudii9Author Commented:
public String wordEnds(String str, String word) {
int xyPos=str.indexOf("XY");
		int xyPosition=str.lastIndexOf("XY");
		//System.out.println("xyPos"+xyPos);
		//System.out.println("xyPosition"+xyPosition);
		String s1=str.substring(xyPos-1,xyPos);
		String s2=str.substring(xyPos+2,xyPos+3);
		//String s2=str.replaceAll(str,word);
		String s3=str.substring(xyPosition-1,xyPosition);
		String s4=str.substring(xyPosition+2,xyPosition+3);
		//System.out.println("s1 is"+s1);
		//System.out.println("s2 is"+s2);
		//System.out.println("s3 is"+s3);
		//System.out.println("s4 is"+s4);
		//String s4=str.replaceAll(str,word);
		//String s5=s1+s2;
		//String s6=s3+s4;
		String s7=s1+s2+s3+s4;
		return s7;
  
}

Open in new window


above failing test cases as below.
Expected	Run		
wordEnds("abcXY123XYijk", "XY") → "c13i"	"c13i"	OK	    
wordEnds("XY123XY", "XY") → "13"	"Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:6)"	X	    
wordEnds("XY1XY", "XY") → "11"	"Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:6)"	X	    
wordEnds("XYXY", "XY") → "XY"	"Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:6)"	X	    
wordEnds("XY", "XY") → ""	"Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:6)"	X	    
wordEnds("Hi", "XY") → ""	"Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -2 (line number:6)"	X	    
wordEnds("", "XY") → ""	"Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -2 (line number:6)"	X	    
wordEnds("abc1xyz1i1j", "1") → "cxziij"	"Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -2 (line number:6)"	X	    
wordEnds("abc1xyz1", "1") → "cxz"	"Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -2 (line number:6)"	X	    
wordEnds("abc1xyz11", "1") → "cxz11"	"Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -2 (line number:6)"	X	    
wordEnds("abc1xyz1abc", "abc") → "11"	"Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -2 (line number:6)"	X	    
wordEnds("abc1xyz1abc", "b") → "acac"	"Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -2 (line number:6)"	X	    
wordEnds("abc1abc1abc", "abc") → "1111"	"Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -2 (line number:6)"	X	    
other tests
X	 

Open in new window

please advise
gudii9Author Commented:
public String wordEnds(String str, String word) {
int xyPos=str.indexOf("XY");
		int xyPosition=str.lastIndexOf("XY");
		//System.out.println("xyPos"+xyPos);
		//System.out.println("xyPosition"+xyPosition);
if(xyPos>=1 && xyPosition>=1){
		String s1=str.substring(xyPos-1,xyPos);
		String s2=str.substring(xyPos+2,xyPos+3);
		//String s2=str.replaceAll(str,word);
		String s3=str.substring(xyPosition-1,xyPosition);
		String s4=str.substring(xyPosition+2,xyPosition+3);
		//System.out.println("s1 is"+s1);
		//System.out.println("s2 is"+s2);
		//System.out.println("s3 is"+s3);
		//System.out.println("s4 is"+s4);
		//String s4=str.replaceAll(str,word);
		//String s5=s1+s2;
		//String s6=s3+s4;
		String s7=s1+s2+s3+s4;
		return s7;
}
else
return "hello";
  
}

Open in new window

Expected      Run            
wordEnds("abcXY123XYijk", "XY") → "c13i"      "c13i"      OK         
wordEnds("XY123XY", "XY") → "13"      "hello"      X         
wordEnds("XY1XY", "XY") → "11"      "hello"      X         
wordEnds("XYXY", "XY") → "XY"      "hello"      X         
wordEnds("XY", "XY") → ""      "hello"      X         
wordEnds("Hi", "XY") → ""      "hello"      X         
wordEnds("", "XY") → ""      "hello"      X         
wordEnds("abc1xyz1i1j", "1") → "cxziij"      "hello"      X         
wordEnds("abc1xyz1", "1") → "cxz"      "hello"      X         
wordEnds("abc1xyz11", "1") → "cxz11"      "hello"      X         
wordEnds("abc1xyz1abc", "abc") → "11"      "hello"      X         
wordEnds("abc1xyz1abc", "b") → "acac"      "hello"      X         
wordEnds("abc1abc1abc", "abc") → "1111"      "hello"      X         
other tests
X

how to fix above tests.
gudii9Author Commented:
public class WordEndsEx {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

      // System.out.println("value is----"+wordEnds("abcXY123XYijk", "XY")); //"c13i"
       System.out.println("value is----"+wordEnds("XY123XY", "XY")); //"13"
	}

	
	public static String wordEnds(String str, String word) {
	int xyPos=str.indexOf("XY");
	int xyPosition=str.lastIndexOf("XY");
	//System.out.println("xyPos"+xyPos);
	//System.out.println("xyPosition"+xyPosition);
if(xyPos>=1 && xyPosition>=1){
	String s1=str.substring(xyPos-1,xyPos);
	String s2=str.substring(xyPos+2,xyPos+3);
	//String s2=str.replaceAll(str,word);
	String s3=str.substring(xyPosition-1,xyPosition);
	String s4=str.substring(xyPosition+2,xyPosition+3);
	//System.out.println("s1 is"+s1);
	//System.out.println("s2 is"+s2);
	//System.out.println("s3 is"+s3);
	//System.out.println("s4 is"+s4);
	//String s4=str.replaceAll(str,word);
	//String s5=s1+s2;
	//String s6=s3+s4;
	String s7=s1+s2+s3+s4;
	return s7;
}
else if(xyPos==0 && (    xyPosition>=1&&(  (str.length()-xyPosition >=3)) )    ){
	String s1="";
	System.out.println("s1 is-->"+s1);
	String s2=str.substring(xyPos+2,xyPos+3);
	System.out.println("s2 is-->"+s2);
	//String s2=str.replaceAll(str,word);
	
	String s3=str.substring(xyPosition-1,xyPosition);
	System.out.println("s3 is-->"+s3);
	String s4=str.substring(xyPosition+2,xyPosition+3);
	System.out.println("s4 is-->"+s4);
	//System.out.println("s1 is"+s1);
	//System.out.println("s2 is"+s2);
	//System.out.println("s3 is"+s3);
	//System.out.println("s4 is"+s4);
	//String s4=str.replaceAll(str,word);
	//String s5=s1+s2;
	//String s6=s3+s4;
	String s7=s1+s2+s3+s4;
	return s7;
}


else if(xyPos>=1 && xyPosition==0){
	String s1=str.substring(xyPos-1,xyPos);
	String s2=str.substring(xyPos+2,xyPos+3);
	//String s2=str.replaceAll(str,word);
	String s3=str.substring(xyPosition-1,xyPosition);
	String s4="";
	//System.out.println("s1 is"+s1);
	//System.out.println("s2 is"+s2);
	//System.out.println("s3 is"+s3);
	//System.out.println("s4 is"+s4);
	//String s4=str.replaceAll(str,word);
	//String s5=s1+s2;
	//String s6=s3+s4;
	String s7=s1+s2+s3+s4;
	return s7;
}

else
return "hello";}
}

Open in new window


tried like above but not working
gudii9Author Commented:
public class WordEndsEx {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

      // System.out.println("value is----"+wordEnds("abcXY123XYijk", "XY")); //"c13i"
     //  System.out.println("value is----"+wordEnds("XY123XY", "XY")); //"13"
       //System.out.println("value is----"+wordEnds("hi", "XY")); //""
       System.out.println("value is---"+wordEnds("abc1xyz1i1j", "1"));//"cxziij"
	}

	
	public static String wordEnds(String str, String word) {
		int xyPos=str.indexOf(word);
	int xyPosition=str.lastIndexOf(word);
	System.out.println("xyPos"+xyPos);
	System.out.println("xyPosition"+xyPosition);
if(xyPos>=1 && xyPosition>=1){
	String s1=str.substring(xyPos-1,xyPos);
	String s2;
	if(str.length()-xyPosition>=3){
	s2=str.substring(xyPos+2,xyPos+3);
	}
	else{
		s2="";
	}
		
	//String s2=str.replaceAll(str,word);
	String s3=str.substring(xyPosition-1,xyPosition);
	//String s4=str.substring(xyPosition+2,xyPosition+3);
	String s4;
	if(str.length()-xyPosition>=3){
	s4=str.substring(xyPos+2,xyPos+3);
	}
	else{
		s4="";
	}
	//System.out.println("s1 is"+s1);
	//System.out.println("s2 is"+s2);
	//System.out.println("s3 is"+s3);
	//System.out.println("s4 is"+s4);
	//String s4=str.replaceAll(str,word);
	//String s5=s1+s2;
	//String s6=s3+s4;
	String s7=s1+s2+s3+s4;
	return s7;
}
else if(xyPos<=0 && xyPosition>=1){
	String s1="";
	//System.out.println("s1 is-->"+s1);
	String s2=str.substring(xyPos+2,xyPos+3);
	//System.out.println("s2 is-->"+s2);
	//String s2=str.replaceAll(str,word);
	
	String s3=str.substring(xyPosition-1,xyPosition);
	//System.out.println("s3 is-->"+s3);
	String s4;
	if(str.length()-xyPosition >=3){
	s4=str.substring(xyPosition+2,xyPosition+3);
	}else{
		s4="";
	}
	//System.out.println("s4 is-->"+s4);
	//System.out.println("s1 is"+s1);
	//System.out.println("s2 is"+s2);
	//System.out.println("s3 is"+s3);
	//System.out.println("s4 is"+s4);
	//String s4=str.replaceAll(str,word);
	//String s5=s1+s2;
	//String s6=s3+s4;
	String s7=s1+s2+s3+s4;
	return s7;
}


else if(xyPos>=1 && xyPosition<=0){
	String s1=str.substring(xyPos-1,xyPos);
	String s2=str.substring(xyPos+2,xyPos+3);
	//String s2=str.replaceAll(str,word);
	String s3=str.substring(xyPosition-1,xyPosition);
	String s4="";
	//System.out.println("s1 is"+s1);
	//System.out.println("s2 is"+s2);
	//System.out.println("s3 is"+s3);
	//System.out.println("s4 is"+s4);
	//String s4=str.replaceAll(str,word);
	//String s5=s1+s2;
	//String s6=s3+s4;
	String s7=s1+s2+s3+s4;
	return s7;
}


else if(xyPos<=0 && xyPosition<=0){
	System.out.println("here");
	return "";
}

else
return "hello";}
}

Open in new window


i got wrong output
xyPos3
xyPosition9
value is---ci
public String wordEnds(String str, String word) {

	int xyPos=str.indexOf("XY");
	int xyPosition=str.lastIndexOf("XY");
	//System.out.println("xyPos"+xyPos);
	//System.out.println("xyPosition"+xyPosition);
       if(xyPos>=1 && xyPosition>=1){
	String s1=str.substring(xyPos-1,xyPos);
	String s2=str.substring(xyPos+2,xyPos+3);
	//String s2=str.replaceAll(str,word);
	String s3=str.substring(xyPosition-1,xyPosition);
	String s4=str.substring(xyPosition+2,xyPosition+3);
	//System.out.println("s1 is"+s1);
	//System.out.println("s2 is"+s2);
	//System.out.println("s3 is"+s3);
	//System.out.println("s4 is"+s4);
	//String s4=str.replaceAll(str,word);
	//String s5=s1+s2;
	//String s6=s3+s4;
	String s7=s1+s2+s3+s4;
	return s7;
}
else if(xyPos<=0 && xyPosition>=1){
	String s1="";
	//System.out.println("s1 is-->"+s1);
	String s2=str.substring(xyPos+2,xyPos+3);
	//System.out.println("s2 is-->"+s2);
	//String s2=str.replaceAll(str,word);
	
	String s3=str.substring(xyPosition-1,xyPosition);
	//System.out.println("s3 is-->"+s3);
	String s4;
	if(str.length()-xyPosition >=3){
	s4=str.substring(xyPosition+2,xyPosition+3);
	}else{
		s4="";
	}
	//System.out.println("s4 is-->"+s4);
	//System.out.println("s1 is"+s1);
	//System.out.println("s2 is"+s2);
	//System.out.println("s3 is"+s3);
	//System.out.println("s4 is"+s4);
	//String s4=str.replaceAll(str,word);
	//String s5=s1+s2;
	//String s6=s3+s4;
	String s7=s1+s2+s3+s4;
	return s7;
}


else if(xyPos>=1 && xyPosition<=0){
	String s1=str.substring(xyPos-1,xyPos);
	String s2=str.substring(xyPos+2,xyPos+3);
	//String s2=str.replaceAll(str,word);
	String s3=str.substring(xyPosition-1,xyPosition);
	String s4="";
	//System.out.println("s1 is"+s1);
	//System.out.println("s2 is"+s2);
	//System.out.println("s3 is"+s3);
	//System.out.println("s4 is"+s4);
	//String s4=str.replaceAll(str,word);
	//String s5=s1+s2;
	//String s6=s3+s4;
	String s7=s1+s2+s3+s4;
	return s7;
}


else if(xyPos<=0 && xyPosition<=0){
	
	return "";
}

else
return "hello";
  
}

Open in new window


Expected      Run            
wordEnds("abcXY123XYijk", "XY") → "c13i"      "c13i"      OK         
wordEnds("XY123XY", "XY") → "13"      "13"      OK         
wordEnds("XY1XY", "XY") → "11"      "11"      OK         
wordEnds("XYXY", "XY") → "XY"      "XY"      OK         
wordEnds("XY", "XY") → ""      ""      OK         
wordEnds("Hi", "XY") → ""      ""      OK         
wordEnds("", "XY") → ""      ""      OK         
wordEnds("abc1xyz1i1j", "1") → "cxziij"      ""      X         
wordEnds("abc1xyz1", "1") → "cxz"      ""      X         
wordEnds("abc1xyz11", "1") → "cxz11"      ""      X         
wordEnds("abc1xyz1abc", "abc") → "11"      ""      X         
wordEnds("abc1xyz1abc", "b") → "acac"      ""      X         
wordEnds("abc1abc1abc", "abc") → "1111"      ""      X         
other tests
X         
Your progress graph for this problem

please advise
gudii9Author Commented:
how to write without using regular expression?
ozoCommented:
public String wordEnds(String str, String word) {
  String ends="";
  for( int i=0; i<str.length(); ++i ){
     if( str.startsWith(word,i) ){
       if( i>0 ){
          ends += str.substring(i-1,i);
       }
       if( i+word.length()<str.length() ){
           ends += str.substring(i+word.length(),i+word.length()+1);
       }
     }
  }
  return ends;
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gudii9Author Commented:
let me understand
gudii9Author Commented:
public class WordEnds {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		/*
		 * Given a string and a non-empty word string, return a string made of
		 * each char just before and just after every appearance of the word in
		 * the string. Ignore cases where there is no char before or after the
		 * word, and a char may be included twice if it is between two words.
		 * 
		 * wordEnds("abcXY123XYijk", "XY") → "c13i" wordEnds("XY123XY", "XY") →
		 * "13" wordEnds("XY1XY", "XY") → "11"
		 */
		System.out.println("is--->" + wordEnds("abcXY123XYijk", "XY"));

	}

	public static String wordEnds(String str, String word) {
		String ends = "";
		for (int i = 0; i < str.length(); ++i) {
			if (str.startsWith(word, i)) {
				if (i > 0) {
					ends += str.substring(i - 1, i);
					System.out.println("end is--->"+ends);
				}
				if (i + word.length() < str.length()) {
					ends += str.substring(i + word.length(), i + word.length() + 1);
					System.out.println("end isss--->"+ends);
				}
			}
		}
		return ends;
	}

}

Open in new window


above gave below output
end is--->c
end isss--->c1
end is--->c13
end isss--->c13i
is--->c13i
gudii9Author Commented:
when i try to debug it is strting with i=3 not i=0 for unknown reason. please advise
IValue.jpg
ozoCommented:
when str="abcXY123XYijk", word="XY", and i=0,
what is str.startsWith(word, i)?
gudii9Author Commented:
when str="abcXY123XYijk", word="XY", and i=0,
what is str.startsWith(word, i)?

true right as XY is at index 3 but i stilll though eclipse debugger should have strted with i=0 not i=3. please advise
ozoCommented:
If you are looking at line 25, you don't  get to line 25 when i=0
gudii9Author Commented:
If you are looking at line 25, you don't  get to line 25 when i=0

so ariable values on right hand side specific to a line number like 25?

i though i got assigned some value at line 23 which should carry forward?

please advise
ozoCommented:
i got assigned the value 0 in line 22
when i was 0, line 25 was not executed
then i got incremeted from 0 to 1 in line 22
when i was 1, line 25 was not executed
then i got incremeted from 1 to 2 in line 22
when i was 2, line 25 was not executed
then i got incremeted from 2 to 3 in line 22
when i was 3, line 25 was executed, and the value of i at that time was 3.
gudii9Author Commented:
by putting break point at 22 i can see right

i got assigned the value 0 in line 22
when i was 0, line 25 was not executed
then i got changed from 0 to 1 in line 22
when i was 1, line 25 was not executed
then i got changed from 1 to 2 in line 22
when i was 2, line 25 was not executed
then i got changed from 2 to 3 in line 22
when i was 3, line 25 was executed, and the value of i at that time was 3.
gudii9Author Commented:
public String wordEnds(String str, String word) {
   return str.replaceAll("(?:(?!.?"+word+").)*(?:(.)?"+word+"(?=(.)"+word+")?"+"(?="+word+"(?<=(.)))?(.)?)?","$1$2$4$3");
}
// this could be easier using matcher.group() instead of replaceAll(), but I don't see a way to include java.util.regex.Matcher from codingbat

can you please advise on above. I was not clear what is going on?
ozoCommented:
I don't think that's a good way to do it, it's too awkward to reconcile the way the challenge wants to handle overlaps within the way replaceAll wants to handle them.
(it might be a little easier if there was a character that was guaranteed to never occur in str, but it's still probably easier to do this challenge with a loop)
gudii9Author Commented:
if (i + word.length() < str.length()

i wonder why we are checking above if condition?

i see
for loop has one top level if which has two bottom  level if loops which are filling before and after character of xy ie word

      if (i > 0) {
                              ends += str.substring(i - 1, i);
                              System.out.println("end is--->"+ends);
                        }
                        if (i + word.length() < str.length()) {
                              ends += str.substring(i + word.length(), i + word.length() + 1);
                              System.out.println("end isss--->"+ends);
                        }

public class WordEnds {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		/*
		 * Given a string and a non-empty word string, return a string made of
		 * each char just before and just after every appearance of the word in
		 * the string. Ignore cases where there is no char before or after the
		 * word, and a char may be included twice if it is between two words.
		 * 
		 * wordEnds("abcXY123XYijk", "XY") → "c13i" wordEnds("XY123XY", "XY") →
		 * "13" wordEnds("XY1XY", "XY") → "11"
		 */
		System.out.println("is--->" + wordEnds("abcXY123XYijk", "XY"));

	}

	public static String wordEnds(String str, String word) {
		String ends = "";
		for (int i = 0; i < str.length(); ++i) {
			if (str.startsWith(word, i)) {
				if (i > 0) {
					ends += str.substring(i - 1, i);
					System.out.println("end is--->"+ends);
				}
				if (i + word.length() < str.length()) {
					ends += str.substring(i + word.length(), i + word.length() + 1);
					System.out.println("end isss--->"+ends);
				}
			}
		}
		return ends;
	}

}

Open in new window

gudii9Author Commented:
public class WordEnds {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		/*
		 * Given a string and a non-empty word string, return a string made of
		 * each char just before and just after every appearance of the word in
		 * the string. Ignore cases where there is no char before or after the
		 * word, and a char may be included twice if it is between two words.
		 * 
		 * wordEnds("abcXY123XYijk", "XY") → "c13i" wordEnds("XY123XY", "XY") →
		 * "13" wordEnds("XY1XY", "XY") → "11"
		 */
		System.out.println("is--->" + wordEnds("abcXY123XYijk", "XY"));

	}

	public static String wordEnds(String str, String word) {
		String ends = "";
		for (int i = 0; i < str.length(); ++i) {
			if (str.startsWith(word, i)) {
				if (i > 0) {
					ends += str.substring(i - 1, i);
					System.out.println("end is--->"+ends);
				}
				if (i > 0) {//here i changed condition as i>0
					ends += str.substring(i + word.length(), i + word.length() + 1);
					System.out.println("end isss--->"+ends);
				}
			}
		}
		return ends;
	}

}

Open in new window


above also worked fine gave same output
end is--->c
end isss--->c1
end is--->c13
end isss--->c13i
is--->c13i
ozoCommented:
with
if (i > 0) {//here i changed condition as i>0
Expected      Run
wordEnds("XY123XY", "XY") → "13"      "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 8 (line number:10)"
gudii9Author Commented:
public class WordEnds {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		/*
		 * Given a string and a non-empty word string, return a string made of
		 * each char just before and just after every appearance of the word in
		 * the string. Ignore cases where there is no char before or after the
		 * word, and a char may be included twice if it is between two words.
		 * 
		 * wordEnds("abcXY123XYijk", "XY") → "c13i" wordEnds("XY123XY", "XY") →
		 * "13" wordEnds("XY1XY", "XY") → "11"
		 */
		System.out.println("is--->" + wordEnds("XY123XY", "XY"));

	}

	public static String wordEnds(String str, String word) {
		String ends = "";
		for (int i = 0; i < str.length(); ++i) {
			if (str.startsWith(word, i)) {
				if (i > 0) {
					ends += str.substring(i - 1, i);
					System.out.println("end is--->"+ends);
				}
				if (i > 0) {//here i changed condition as i>0
					ends += str.substring(i + word.length(), i + word.length() + 1);
					System.out.println("end isss--->"+ends);
				}
			}
		}
		return ends;
	}

}

Open in new window


correct above failed with below message
end is--->3
Exception in thread "main" java.lang.StringIndexOutOfBoundsException
      at java.lang.String.substring(String.java:1148)
      at WordEnds.wordEnds(WordEnds.java:28)
      at WordEnds.main(WordEnds.java:15)
gudii9Author Commented:
so below is needed in bottome if to pass all tests?

if(i + word.length() < str.length()) not if if (i > 0)
public class WordEnds {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		/*
		 * Given a string and a non-empty word string, return a string made of
		 * each char just before and just after every appearance of the word in
		 * the string. Ignore cases where there is no char before or after the
		 * word, and a char may be included twice if it is between two words.
		 * 
		 * wordEnds("abcXY123XYijk", "XY") → "c13i" wordEnds("XY123XY", "XY") →
		 * "13" wordEnds("XY1XY", "XY") → "11"
		 */
		System.out.println("is--->" + wordEnds("XY123XY", "XY"));

	}

	public static String wordEnds(String str, String word) {
		String ends = "";
		for (int i = 0; i < str.length(); ++i) {
			if (str.startsWith(word, i)) {
				if (i > 0) {
					ends += str.substring(i - 1, i);
					System.out.println("end is--->"+ends);
				}
				if (i > 0) {//here i changed condition as i>0 not if(i + word.length() < str.length())
					ends += str.substring(i + word.length(), i + word.length() + 1);
					System.out.println("end isss--->"+ends);
				}
			}
		}
		return ends;
	}

}

Open in new window

gudii9Author Commented:
public class WordEnds {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		/*
		 * Given a string and a non-empty word string, return a string made of
		 * each char just before and just after every appearance of the word in
		 * the string. Ignore cases where there is no char before or after the
		 * word, and a char may be included twice if it is between two words.
		 * 
		 * wordEnds("abcXY123XYijk", "XY") → "c13i" wordEnds("XY123XY", "XY") →
		 * "13" wordEnds("XY1XY", "XY") → "11"
		 */
		System.out.println("is--->" + wordEnds("XY123XY", "XY"));

	}

	public static String wordEnds(String str, String word) {
		String ends = "";
		for (int i = 0; i < str.length(); ++i) {
			if (str.startsWith(word, i)) {
				if (i > 0) {
					ends += str.substring(i - 1, i);
					System.out.println("end is--->"+ends);
				}
				if(i + word.length() < str.length()) {//here i changed condition as i>0 not if(i + word.length() < str.length())
					ends += str.substring(i + word.length(), i + word.length() + 1);
					System.out.println("end isss--->"+ends);
				}
			}
		}
		return ends;
	}

}

Open in new window


above passed that test
end isss--->1
end is--->13
is--->13
gudii9Author Commented:
wordEnds("XY123XY", "XY"));

wordEnds("abcXY123XYijk", "XY")

what is difference between above two. why first one failed with if (i > 0) { at bottom if loop?
gudii9Author Commented:
if(i + word.length() < str.length()

what is meaning of above line?
ozoCommented:
It allows
ends += str.substring(i + word.length(), i + word.length() + 1);
to executed only when str.substring(i + word.length(), i + word.length() + 1) exists
gudii9Author Commented:
http://www.tutorialspoint.com/java/java_string_startswith.htm

i see you are using method with offset

public class WordEnds {

      public static void main(String[] args) {
            // TODO Auto-generated method stub

            /*
             * Given a string and a non-empty word string, return a string made of
             * each char just before and just after every appearance of the word in
             * the string. Ignore cases where there is no char before or after the
             * word, and a char may be included twice if it is between two words.
             *
             * wordEnds("abcXY123XYijk", "XY") → "c13i" wordEnds("XY123XY", "XY") →
             * "13" wordEnds("XY1XY", "XY") → "11"
             */
            System.out.println("is--->" + wordEnds("abc1xyz1i1j", "1"));// abcXY123XYijk

      }

      public static String wordEnds(String str, String word) {
              String ends="";
              for( int i=0; i<str.length(); i++ ){
                 if( str.startsWith(word,i) ){
                   if( i>0 ){
                      ends += str.substring(i-1,i);
                   }
                   if( i+word.length()<str.length() ){
                       ends += str.substring(i+word.length(),i+word.length()+1);
                   }
                 }
              }
              return ends;
            }

}

above gave same output as below even after changing ++i to i++

public class WordEnds {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		/*
		 * Given a string and a non-empty word string, return a string made of
		 * each char just before and just after every appearance of the word in
		 * the string. Ignore cases where there is no char before or after the
		 * word, and a char may be included twice if it is between two words.
		 * 
		 * wordEnds("abcXY123XYijk", "XY") → "c13i" wordEnds("XY123XY", "XY") →
		 * "13" wordEnds("XY1XY", "XY") → "11"
		 */
		System.out.println("is--->" + wordEnds("abc1xyz1i1j", "1"));// abcXY123XYijk

	}

	public static String wordEnds(String str, String word) {
		  String ends="";
		  for( int i=0; i<str.length(); i++ ){
		     if( str.startsWith(word,i) ){
		       if( i>0 ){
		          ends += str.substring(i-1,i);
		       }
		       if( i+word.length()<str.length() ){
		           ends += str.substring(i+word.length(),i+word.length()+1);
		       }
		     }
		  }
		  return ends;
		}

}

Open in new window


is--->cxziij


can i use i++ instead of ++i .

please advise
gudii9Author Commented:
public class WordEnds {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		/*
		 * Given a string and a non-empty word string, return a string made of
		 * each char just before and just after every appearance of the word in
		 * the string. Ignore cases where there is no char before or after the
		 * word, and a char may be included twice if it is between two words.
		 * 
		 * wordEnds("abcXY123XYijk", "XY") → "c13i" wordEnds("XY123XY", "XY") →
		 * "13" wordEnds("XY1XY", "XY") → "11"
		 */
		System.out.println("is--->" + wordEnds("abc1xyz1i1j", "1"));// abcXY123XYijk

	}

	public static String wordEnds(String str, String word) {
		  String ends="";
		  for( int i=0; i<str.length(); i++ ){
		     if( str.startsWith(word,i) ){
		       if( i>0 ){
		          ends += str.substring(i-1,i);
		       }
		       if( i+word.length()<str.length() ){//3+1<11
		           ends += str.substring(i+word.length(),i+word.length()+1);//3+1,3+1+1
		       }
		     }
		  }
		  return ends;
		}

}

Open in new window


above fails below
Expected      Run            
wordEnds("abcXY123XYijk", "XY") → "c13i"      "c13i"      OK         
wordEnds("XY123XY", "XY") → "13"      "13"      OK         
wordEnds("XY1XY", "XY") → "11"      "11"      OK         
wordEnds("XYXY", "XY") → "XY"      "XY"      OK         
wordEnds("XY", "XY") → ""      ""      OK         
wordEnds("Hi", "XY") → ""      ""      OK         
wordEnds("", "XY") → ""      ""      OK         
wordEnds("abc1xyz1i1j", "1") → "cxziij"      ""      X         
wordEnds("abc1xyz1", "1") → "cxz"      ""      X         
wordEnds("abc1xyz11", "1") → "cxz11"      ""      X         
wordEnds("abc1xyz1abc", "abc") → "11"      ""      X         
wordEnds("abc1xyz1abc", "b") → "acac"      ""      X         
wordEnds("abc1abc1abc", "abc") → "1111"      ""      X         
other tests
X         
Your progress graph for this problem
gudii9Author Commented:
It allows
ends += str.substring(i + word.length(), i + word.length() + 1);
to executed only when str.substring(i + word.length(), i + word.length() + 1) exists

when str.substring(i + word.length(), i + word.length() + 1) does not exist?
ozoCommented:
when str.substring(i + word.length(), i + word.length() + 1) does not exist, trying to access it would cause java.lang.StringIndexOutOfBoundsException
gudii9Author Commented:
It mean by does not exist?
ozoCommented:
What would "XY".substring(3,4) be?
gudii9Author Commented:
"XY".substring(3,4) be?  means stringoutofbound exception as after index1 no more characters in the string xy
gudii9Author Commented:
str.substring(i+word.length(),i+word.length()+1)

i see str or xy length if greater than i+word.length(),i+word.length()+1 ie 3,4 for example then only we cando substring..
gudii9Author Commented:
can i use i++ instead of ++i .
Does it make any difference here?
please advise.
ozoCommented:
i++ instead of ++i makes no difference between
for( int i=0; i<str.length(); i++ ){
and
for( int i=0; i<str.length(); ++i ){
gudii9Author Commented:
i++ instead of ++i makes no difference between
for( int i=0; i<str.length(); i++ ){
and
for( int i=0; i<str.length(); ++i ){

just here or any where?
ozoCommented:
When the resulting value is not used.
gudii9Author Commented:
When the resulting value is not used.
here we are using i within if condition right?
ozoCommented:
The value of i is used in i<str.length();
The value of i++ is not used.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.