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

asked on

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
Avatar of gheist
gheist
Flag of Belgium image

Improvement to approach would be to do yourself instead of asking others to pass your exam.
Avatar of ozo
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
Avatar of gudii9

ASKER

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?
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"
Avatar of gudii9

ASKER

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
Avatar of gudii9

ASKER

i think now i understand
we want each before each after characters for every occurence of word..
Avatar of gudii9

ASKER

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
Avatar of gudii9

ASKER

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
Avatar of gudii9

ASKER

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.
Avatar of gudii9

ASKER

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
Avatar of gudii9

ASKER

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
Avatar of gudii9

ASKER

how to write without using regular expression?
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America 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

let me understand
Avatar of gudii9

ASKER

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
Avatar of gudii9

ASKER

when i try to debug it is strting with i=3 not i=0 for unknown reason. please advise
IValue.jpg
when str="abcXY123XYijk", word="XY", and i=0,
what is str.startsWith(word, i)?
Avatar of gudii9

ASKER

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
If you are looking at line 25, you don't  get to line 25 when i=0
Avatar of gudii9

ASKER

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
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.
Avatar of gudii9

ASKER

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.
Avatar of gudii9

ASKER

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?
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)
Avatar of gudii9

ASKER

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

Avatar of gudii9

ASKER

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
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)"
Avatar of gudii9

ASKER

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)
Avatar of gudii9

ASKER

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

Avatar of gudii9

ASKER

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
Avatar of gudii9

ASKER

wordEnds("XY123XY", "XY"));

wordEnds("abcXY123XYijk", "XY")

what is difference between above two. why first one failed with if (i > 0) { at bottom if loop?
Avatar of gudii9

ASKER

if(i + word.length() < str.length()

what is meaning of above line?
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
Avatar of gudii9

ASKER

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
Avatar of gudii9

ASKER

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
Avatar of gudii9

ASKER

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?
when str.substring(i + word.length(), i + word.length() + 1) does not exist, trying to access it would cause java.lang.StringIndexOutOfBoundsException
Avatar of gudii9

ASKER

It mean by does not exist?
What would "XY".substring(3,4) be?
Avatar of gudii9

ASKER

"XY".substring(3,4) be?  means stringoutofbound exception as after index1 no more characters in the string xy
Avatar of gudii9

ASKER

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..
Avatar of gudii9

ASKER

can i use i++ instead of ++i .
Does it make any difference here?
please advise.
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 ){
Avatar of gudii9

ASKER

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?
When the resulting value is not used.
Avatar of gudii9

ASKER

When the resulting value is not used.
here we are using i within if condition right?
The value of i is used in i<str.length();
The value of i++ is not used.