For further help with Coding Bat (Java), please check out my books. I am also available for tutoring.
startWord:
public String startWord(String str, String word) { if (word.length() > str.length()) return ""; if (str.substring(0, word.length()).equals(word)) return word; if (str.substring(1, word.length()).equals(word.substring(1))) return str.charAt(0) + word.substring(1); return ""; }
withoutX:
public String withoutX(String str) { if (str.length() == 0) return ""; if (str.charAt(0) == 'x') str = str.substring(1); if (str.length() > 0 && str.charAt(str.length() - 1) == 'x') str = str.substring(0, str.length() - 1); return str; }
withoutX2:
public String withoutX2(String str) {
if (str.length() < 2) return "";String result = "";
if (str.charAt(0) != 'x') result += str.charAt(0);
if (str.charAt(1) != 'x') result += str.charAt(1);
result += str.substring(2);
return result;
}
[/sourcecode]The website said that "withoutX2" was a bit trickier than it looks like. However, the key is to not directly manipulate the input string but instead create a new one. Then this exercise is simpler than the preceding one.
For further help with Coding Bat (Java), please check out my books. I am also available for tutoring.
There is a little problem with withoutX2 function.
If you put any parameter containing single sign different than “x”, the function will return empty string.
As far as I understand the task, in this case the function should return copy of the parameter.
Your function: withoutX2(“A”) -> “”
Should be: withoutX2(“A”) -> “A”
It is true the withoutX2 would not compute properly for a value of “x” with the example given. Here is how I did it:
public String withoutX2(String str) {
if(str.length()==1 && str.substring(0,1).equals(“x”)) return “”;
if(str.length()>=2){
if(str.substring(0,2).equals(“xx”)) return str.substring(2);
if(str.substring(0,1).equals(“x”)) return str.substring(1);
if(str.substring(1,2).equals(“x”)) return str.substring(0,1)+str.substring(2);
}
return str;
}
P.S. Thanks for posting this, been running through these to get java fresh in my mind again and really helped having these when I got stuck.
I find a better solution to withoutX2 is this:
public String withoutX2(String str) {
if(str.length()>1){
String temp = str.substring(0,2);
str = str.substring(2);
temp = temp.replace("x", "");
return temp+str;
}
if (str.length()==1 && str.charAt(0)=='x')
return "";
return str;
}
How is this supposed to be better than my version? Besides, you shouldn’t use the inbuilt method replace() in this exercise.
God bless whoever made this
I decided as follows:
public String withoutX2(String str) {
if (str.length() < 2) return "";
if (str.charAt(1) == 'x') {str = str.substring(0, 1) + str.substring(2);}
if (str.charAt(0) == 'x') {str = str.substring(1);}
return str;
}
Though it looks like your solution^..^
Hi !
Maybe this is a better solution 😀
startWord
public String startWord(String str, String word) {
String ord = word.substring(1);
if(str.length() >= 1){
if(str.indexOf(ord)!= -1){
return str.charAt(0)+””+ ord;
}
}
return “”;
}
Here’s my solution for String-1 > withoutX2
public String withoutX2(String str) {
if(str.length() <= 1){
return "";
}
if(str.charAt(0) == 'x' && str.charAt(1) == 'x'){
return(str.substring(2));
}
if(str.charAt(1) == 'x'){
return(str.substring(0,1) + str.substring(str.length()- 1));
}
if(str.charAt(0) == 'x'){
return(str.substring(1));
}
return str;
}