Friday 29 July 2016

plusout

Goto Problem

Given a string and a non-empty word string, return a version of the original String where all chars have been replaced by pluses ("+"), except for appearances of the word string which are preserved unchanged.

plusOut("12xy34", "xy") → "++xy++"
plusOut("12xy34", "1") → "1+++++"
plusOut("12xy34xyabcxy", "xy") → "++xy++xy+++xy"

public String plusOut(String str, String word)
 {
    String res="";
    int i=0;
    while(i<str.length())
    {
       if(str.substring(i).startsWith(word))
       {
           res=res+word;
           i=i+word.length();
       }
       else 
       {
          res=res+"+";i++;
       }
    }
    return res;
 }

No comments:

Post a Comment