Reputation:
i posted the other day with practice questions i was getting stuck with and i am stuck again
Please firstly can i ask you dont post full solutions.
The question is from here
http://www.javabat.com/prob/p141494
and reads
Given a string and a second "word" string, we'll say that the word matches the string if it appears at the front of the string, except its first char does not need to match exactly. On a match, return the front of the string, or otherwise return the empty string. So, so with the string "hippo" the word "hi" returns "hi" and "xip" returns "hip". The word will be at least length 1.
startWord("hippo", "hi") → "hi" startWord("hippo", "xip") → "hip" startWord("hippo", "i") → "h"
I am getting very stuck, the wording of the question isn't helping me! This is the code i have so far
public String startWord(String str, String word)
{
if (str.startsWith(word)){
return str.substring(0, word.length());
}
if (str.substring(1, str.length()).equals(word.substring(1, word.length()))){
return str.substring(0, word.length());
}
return "";
}
hopefully someone will be able to help me out here with a pointer or 2, thank you for the help
Upvotes: 1
Views: 1618
Reputation: 40336
Here's a starting point for you
import junit.framework.TestCase;
public class HippoTest extends TestCase {
public void testActualStart() throws Exception {
assertEquals("hi", startWord("hippo", "hi"));
}
public void testSimilarStart() throws Exception {
assertEquals("xip", startWord("hippo", "hip"));
assertEquals("h", startWord("i", "hip"));
}
public void testWrongStart() throws Exception {
assertEquals("", startWord("hippo", "hx"));
}
private String startWord(String string, String string2) {
// TODO Auto-generated method stub
return null;
}
}
If you make these test cases pass, one at a time, perhaps it will be easier than trying to solve the whole problem at once.
Upvotes: 0
Reputation: 28638
Try using:
if (str.length() > 0 && str.substring(1).startsWith(word.substring(1))) {
for the conditional as it is much clearer. Also, tvanfosson and Darth Eru are correct that the first conditional with the exact match on the first character is extraneous.
Upvotes: 0
Reputation: 5807
you've got the right idea there... simply compare a substring of str from character 1 to word.length to a substring of word from character 1 to the end of word. if they match return the substring of str to word.length.
Upvotes: 2
Reputation: 4880
Your problem in your second IF statement is that you are comparing the word to the entire substring. What you should compare it to (after checking their length of course) is:
str.substring(1,word.length()).equals(word.substring(1,word.length()))
Upvotes: 2
Reputation: 4470
Your problem is in your second comparison (the general case). You are taking a substring of str to the end of str, and comparing it with a substring of word to the end of word. However, if str="hippo" and word="xip", "ippo" != "ip".
As an additional note, once you fix the second case, you won't really need the first case, as it's covered by the second case.
Upvotes: 3
Reputation: 532435
In the case where you are ignoring the first character, you only want to compare word.length() - 1
characters -- and the same length for both the string and the word.
Note that you really don't need to test the case where the first letter exactly matches as that is a subset of the case where you are ignoring the first letter.
Upvotes: 2
Reputation: 58637
You could try using regular expressions, starting at the 2nd character, to see if there is a match. You could also do some sort of substring, as I see you have done.
For the boundary case of a 1 letter long string, just hard code it to return properly.
Upvotes: -2