1

Why does my code which should return true, return false?

The method isSubstring should return true if str2 contains zero or more characters, then all the characters of str1, then zero or more characters. The method should return false if a does not appear anywhere in b.

I am not looking for an alternative way to return correct output, but want to find out why my code is wrong.

public class stringRecursionPractice{
    public static void main(String[] args){
        String str1 = "abc"; 
        String str2 = "abc";
        System.out.println(isSubstring(str1, str2));
    }

    /*
     * Returns the string containing only the first character of the
     * specified string.
     */
    public static String head(String s) {
        return s.substring(0, 1);
    }

    /*
     * Returns the string containing the all the characters but the first
     * character in the specified string.
     */
    public static String tail(String s) {
        return s.substring(1);
    }

    public static boolean isSubstring(String str1, String str2){
    if(str1.length() == 1 && str1.charAt(0) == str2.charAt(0)){
        return true; 
    }

    if(head(str1).equals(head(str2))){
        isSubstring(tail(str1), tail(str2)); 
    }

    return false; 
    }
}
5
  • You should do some debugging. Commented Jul 26, 2015 at 2:33
  • What have you already done to debug your code? Commented Jul 26, 2015 at 2:33
  • I have tried debugging it several times, but it keeps returning false. Commented Jul 26, 2015 at 2:35
  • Second if block, should also contains some return` statement, since in the absence of that, the control always goes to the last return false;. Commented Jul 26, 2015 at 2:36
  • But wouldn't the last recursive call isSubstring("c", "c") return true anyways? Commented Jul 26, 2015 at 2:38

1 Answer 1

3

Your recursive call should return the result:

if(head(str1).equals(head(str2))){
    return isSubstring(tail(str1), tail(str2)); 
}
Sign up to request clarification or add additional context in comments.

5 Comments

But wouldn't the last recursive call isSubstring("c", "c") return true anyways?
@CMSC Yes, it might return true, but you ignore that, so your program reaches the last statement of the isSubstring which is return false;. (the very first isSubstring call reaches that statement)
@Tom Why does it ignore? The very first isSubstring call would get caught in the second if statement if(head(str1).equals(head(str2))). From there it would do recursion until str1 = "c" and str2 ="c". Then it would be caught in the first if statement and return 'true', shouldn't it?
@CMSC And then what? That very first call has done his job calling the isSubstring a second time ... and then? Since you haven't told him to return the result of the recursive call it just keeps executing the method, so it executes the statement return false.
@Tom Oh I see. Thanks so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.