View this PageEdit this PageAttachments to this PageHistory of this PageHomeRecent ChangesSearch the SwikiHelp Guide

Sample Solution Word Ends

public String wordEnds(String str, String word) {
  String retStr = "";
  String prev = null;
  int next = 0;
  int index = 0;
  int end = 0;
  index = str.indexOf(word);
  while (index >= 0)
  {
        if (index == 0 && prev != null)
        {
           retStr += prev;
           prev = null;
        }
        
        if (index !=0)
        {
           retStr = retStr + str.substring(index-1, index);
        }
        next = index + word.length();
        if (next <= str.length() - 1)
        {
           prev = str.substring(next-1, next);
           retStr += str.substring(next, next+1);
        }
        str = str.substring(next);
        index = str.indexOf(word);

   }
   
   return retStr;
 
 }



Link to this Page