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;
}
I am getting below resultExpected Run
wordEnds("abcXY123XYijk", "XY") → "c13i" "Exception:java.lang.StringIndexOutO fBoundsExc eption: String index out of range: -1 (line number:3)" X
wordEnds("XY123XY", "XY") → "13" "Exception:java.lang.StringIndexOutO fBoundsExc eption: String index out of range: -1 (line number:3)" X
wordEnds("XY1XY", "XY") → "11" "Exception:java.lang.StringIndexOutO fBoundsExc eption: String index out of range: -1 (line number:3)" X
wordEnds("XYXY", "XY") → "XY" "Exception:java.lang.StringIndexOutO fBoundsExc eption: String index out of range: -1 (line number:3)" X
wordEnds("XY", "XY") → "" "Exception:java.lang.StringIndexOutO fBoundsExc eption: String index out of range: -1 (line number:3)" X
wordEnds("Hi", "XY") → "" "Exception:java.lang.StringIndexOutO fBoundsExc eption: String index out of range: -1 (line number:3)" X
wordEnds("", "XY") → "" "Exception:java.lang.StringIndexOutO fBoundsExc eption: String index out of range: -1 (line number:3)" X
wordEnds("abc1xyz1i1j", "1") → "cxziij" "Exception:java.lang.StringIndexOutO fBoundsExc eption: String index out of range: -1 (line number:3)" X
wordEnds("abc1xyz1", "1") → "cxz" "Exception:java.lang.StringIndexOutO fBoundsExc eption: String index out of range: -1 (line number:3)" X
wordEnds("abc1xyz11", "1") → "cxz11" "Exception:java.lang.StringIndexOutO fBoundsExc eption: String index out of range: -1 (line number:3)" X
wordEnds("abc1xyz1abc", "abc") → "11" "Exception:java.lang.StringIndexOutO fBoundsExc eption: String index out of range: -1 (line number:3)" X
wordEnds("abc1xyz1abc", "b") → "acac" "Exception:java.lang.StringIndexOutO fBoundsExc eption: String index out of range: -1 (line number:3)" X
wordEnds("abc1abc1abc", "abc") → "1111" "Exception:java.lang.StringIndexOutO fBoundsExc eption: String index out of range: -1 (line number:3)" X
other tests
X
Your progress graph for this problem
ASKER
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"
return a string made of each char just before and just after every appearance of the word in the string. I
ASKER
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;
}
}
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;
}
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
please advise
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";
}
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
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";}
}
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";}
}
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";
}
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
ASKER
ASKER
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;
}
}
ASKER
ASKER
when str="abcXY123XYijk", word="XY", and i=0,
what is str.startsWith(word, i)?
ASKER
If you are looking at line 25, you don't get to line 25 when i=0
ASKER
ASKER
public String wordEnds(String str, String word) {
return str.replaceAll("(?:(?!.?"+word+").)* (?:(.)?"+w ord+"(?=(. )"+word+") ?"+"(?="+w ord+"(?<=( .)))?(.)?) ?","$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
ASKER
if (i + word.length() < str.length()
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;
}
}
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;
}
}
Expected Run
wordEnds("XY123XY", "XY") → "13" "Exception:java.lang.StringIndexOutO fBoundsExc eption: String index out of range: 8 (line number:10)"
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;
}
}
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 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;
}
}
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;
}
}
ASKER
ASKER
if(i + word.length() < str.length()
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() ){
ends += str.substring(i+word.length(),i+word.length()+1);
}
}
}
return ends;
}
}
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;
}
}
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
ASKER
ASKER
ASKER
ASKER
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 ){
ASKER
When the resulting value is not used.here we are using i within if condition right?
Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.
TRUSTED BY