Wednesday, May 7, 2014

Given a string, compute recursively a new string where all the 'x' chars have been removed. noX("xaxb") → "ab" noX("abc") → "abc" noX("xx") → ""

public static String noX(String str) {
String result = "";
if (str == null || str.trim().length() == 0)
return result;
String c = str.substring(0, 1);
str = str.substring(1);
if (!(c.equals("x"))) {
result = c;
}
return result + noX(str);

}

No comments:

Post a Comment