Java > String-2 > wordEnds (CodingBat Solution)

Problem:

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"


Solution:

public String wordEnds(String str, String word) {
  int slen = str.length();
  int wlen = word.length();
  String fin = "";
  
  for (int i = 0; i < slen-wlen+1; i++) { 
    String tmp = str.substring(i,i+wlen);
    if (i > 0 && tmp.equals(word))
      fin += str.substring(i-1,i);
    if (i < slen-wlen && tmp.equals(word))
      fin += str.substring(i+wlen,i+wlen+1);
  }
  return fin;
}


18 comments :

  1. Nice solution, cleaner than what I had(which did work), thanks for posting.

    ReplyDelete
  2. Alternatively:

    public String wordEnds(String str, String word) {
    int strLen = str.length();
    int wordLen = word.length();
    String result = "";

    for (int i = 0; i < strLen-wordLen+1; i++) {
    if (i > 0 && str.substring(i,i+wordLen).equals(word))
    result += str.charAt(i-1);
    if (i < strLen-wordLen && str.substring(i,i+wordLen).equals(word))
    result += str.charAt(i+wordLen);
    }
    return result;
    }

    ReplyDelete
    Replies
    1. logically this code should produce aiobe exception.

      Delete
  3. I know it looks big but it is easy if u read it from top to bottom

    public String wordEnds(String str, String word) {
    String newStr = "";
    if(str.length() < 2 || word.length() == 0)
    return "";
    str = str.replace(word, ".");

    if(str.length() >= 2){
    for(int i = 1; i < str.length()-1; i++){
    if(str.charAt(i) != '.'){
    if(str.charAt(i-1) =='.')
    newStr += str.charAt(i);
    if(str.charAt(i+1) == '.')
    newStr += str.charAt(i);
    }
    if(str.charAt(i) == '.'){
    if(str.charAt(i-1) =='.')
    newStr += word;
    if(str.charAt(i+1) == '.')
    newStr += word;
    }
    }
    if(str.charAt(0) != '.')
    if(str.charAt(1) == '.')
    newStr= str.charAt(0)+ "" + newStr;;

    if(str.charAt(str.length()-1) != '.')
    if(str.charAt(str.length()-2) == '.')
    newStr= newStr+""+ str.charAt(str.length()-1);
    if(str.charAt(str.length()-1) == '.')
    if(str.charAt(str.length()-2) == '.')
    newStr= newStr+""+ word;
    }

    return newStr;
    }

    ReplyDelete
  4. public String wordEnds(String str, String word) {
    String result = "";
    int l = word.length();
    for (int i = 0; i < str.length(); i++) {
    if (str.startsWith(word, i)) {
    result += ( (i-1) >= 0 ) ? str.charAt(i-1) : "";
    result += ( (i+l) < str.length() ) ? str.charAt(i+l) : "";
    }
    }
    return result;
    }


    use if statement in place of trintry operation..

    ReplyDelete
  5. My solution is very similar to the one at the top of the page, except that I didn't use a temporary String variable in the for loop. Here's my logic:

    If i is greater than zero, and i is positioned at the start of the target word, add the preceding character to the result string. If i is less than the length of the string minus the target word length, and i is positioned at the start of the target word, add the character immediately following the target word.

    public String wordEnds(String str, String word) {
    String result = "";
    int sl = str.length();
    int wl = word.length();

    for(int i = 0; i <= sl - wl; i++){
    if(i >= 1 && str.substring(i, i+wl).equals(word)){
    result += str.charAt(i-1);
    }
    if(str.substring(i, i+wl).equals(word) && i < sl-wl ){
    result += str.charAt(i+wl);
    }
    }
    return result;
    }

    ReplyDelete
  6. public String wordEnds(String str, String word) {
    String result="";
    for(int i=0;i0)result+=str.charAt(i-1);
    if(i<str.length()-word.length())result+=str.charAt(i+word.length());


    }
    }return result;
    }

    ReplyDelete
  7. public String wordEnds(String str, String word) {
    String result = "";
    if (str.equals(word)){
    return result;
    }
    if (str.startsWith(word)) {
    result += str.charAt(word.length());
    }
    for (int i = 1; i < str.length() - word.length(); i++) {
    if (str.startsWith(word, i)) {
    result += str.charAt(i - 1);
    result += str.charAt(i + word.length());
    }

    }
    if (str.endsWith(word)){
    result+= str.charAt(str.length()- word.length()-1);
    }
    return result;
    }

    ReplyDelete
  8. I got infra or precheck error what to do

    ReplyDelete
  9. public String wordEnds(String str, String word) {
    int len=word.length();

    String result="";

    for(int i=0;i0 && temp.equals(word))
    {
    result=result+str.substring(i-1,i)+str.substring(i+len,i+len+1);
    }
    }else{
    String temp1 =str.substring(i);
    if(str.length()>2 && temp1.equals(word))
    result=result+str.substring(i-1,i);
    }

    }

    return result;
    }

    ReplyDelete
  10. public String wordEnds(String str, String word) {
    int word_length = word.length();
    String result = "";
    if(word_length == str.length()) return "";
    for(int i = 0; i <= str.length() - word_length; i++){
    if(str.substring(i,i+word_length).equals(word)){
    if(i == 0){
    result += str.substring(word_length,word_length+1);
    }
    else if(i == (str.length() - word_length)){
    result += str.substring(str.length() - word_length - 1,str.length() - word_length);
    }
    else{
    result += (str.substring(i-1,i) + str.substring(i+word_length,i+word_length+1));
    }
    }
    }
    return result;
    }

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

    ReplyDelete
  12. public String wordEnds(String str, String word) {
    return str.replaceAll(".*?(?="+word+")(?<=(.|^))"+word+"(?=(.|$))|.+","$1$2");
    }

    ReplyDelete
  13. public String wordEnds(String str, String word) {

    StringBuilder newStr = new StringBuilder();

    for(int i = 0; i < str.length()-word.length()+1; i++){
    if(i > 0 && str.substring(i,i+word.length()).equals(word))
    newStr.append(str.charAt(i-1));

    if(i < str.length()-word.length() && str.substring(i,i+word.length()).equals(word))
    newStr.append(str.charAt(i+word.length()));
    }


    return newStr.toString();
    }

    ReplyDelete
  14. public String wordEnds(String str, String word) {
    String newstr = "";
    int index = 0;
    int deender = 0;
    int tester = 100;
    if(str.equals(word))return newstr;
    for (int i = 0; i < str.length(); i++) {
    index = str.indexOf(word, deender);
    if (index != tester && index >= 0) {
    tester = index;
    if (index == 0) {
    newstr += str.charAt(index + word.length());
    deender += i + 1;
    } else if (index + word.length() != str.length()) {
    newstr += str.charAt(index - 1);
    newstr += str.charAt(index + word.length());
    deender += i + 1;
    } else {
    newstr += str.charAt(index - 1);
    deender += i + 1;
    }
    } else {
    deender += i;
    }
    }
    return newstr;
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact