tags:

views:

372

answers:

8

Hi, 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

A: 

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.

samoz
+2  A: 

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.

tvanfosson
A: 

Try Regex!

java.lang.String

boolean matches(String regex)
lud0h
+3  A: 

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.

Sean Nyman
+2  A: 

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()))
amischiefr
What I was saying except you gave it away! ;) The OP asked for tips, not the exact solution.
Sean Nyman
+2  A: 

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.

Victor
A: 

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.

laz
A: 

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.

Carl Manaster