Social Icons

Tuesday, May 25, 2010

plusOut : Solutions for Javabat : 22



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"

Source

Solution-

public String plusOut(String s, String word) {
  String end="";
  for(int i=0 ; i<str.length() ;i++){
       
     if(i<= s.length()-word.length() &&
        s.substring(i,i+word.length()).equals(word)){
        end+=word;
        i+= word.length()-1;       
     }
     else
        end+= "+";
  }
  
  return end;
}



 

 
 
Blogger Templates http://slots.to/