In This Program uses the topic are String and StringBuffer and etc.

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. If inputs are "abcXY123XYijk" and "XY", output should be "c13i". If inputs are "XY123XY" and "XY", output should be "13". If inputs are "XY1XY" and "XY", output should be "11"? in java and use the topic String and StringBuffer and so on


2 Answers
1-2 of  2
2 Answers
  • ublic String wordEnds(String str, String word) {
    02  int slen = str.length();
    03  int wlen = word.length();
    04  String fin = "";
    05   
    06  for (int i = 0; i < slen-wlen+1; i++) {
    07    String tmp = str.substring(i,i+wlen);
    08    if (i > 0 && tmp.equals(word))
    09      fin += str.substring(i-1,i);
    10    if (i < slen-wlen && tmp.equals(word))
    11      fin += str.substring(i+wlen,i+wlen+1);
    12  }
    13  return fin;
    14}

Core Java

Didn't get the answer.
Contact people of Talent-Core Java directly by clicking here