0

I'm having trouble figuring out if a smaller string is a substring of a larger string. Example: s1 = boat s2 = steamboat Here is my code. I can only use for loops and charAt.

public static boolean isSubstring(String s1, String s2){
    boolean substringcheck = false;
    int correct = 0;
    for(int i=0; i<s1.length(); i++){
        for(int j=0; j<s2.length(); j++){
            if(s1.charAt(i) == s2.charAt(j)){      
                correct++;
                for(int n=0; n<s1.length(); n++){
                    if(s1.charAt(n) == s2.charAt(j)){
                        correct++;
                    }else{
                        correct = 0;
                    }
                }
            }
        }
    }
    if(correct == s1.length()){
        substringcheck = true;
    }else{
        substringcheck = false;
    }
    return substringcheck;

}

}

I'm confused on what to put after the if statement to check if all characters in the smaller string match with the ones after we find a match in the larger string.

  • 1
    I'm voting to close this question as off-topic because it's a homework with a little effort – nio Nov 05 '15 at 23:57
  • you need another for loop inside your if, a flag variable to determine if you found a substring or you could use a a break statement, then you need to work on your ending conditions in both for loops – nio Nov 06 '15 at 00:01
  • @Alexander well this is different because I can only use for loops and charAt – Jeff Teague Nov 06 '15 at 00:05
  • well, what is your expected output? – Alexander Nov 06 '15 at 00:09
  • 1
    Hey guys - it is a simple programming error. I'm trying to get the OP to figure it out in his own head ... – Stephen C Nov 06 '15 at 00:11
  • Good on @Stephen. Why all the downvotes? He clearly stated what he's trying to accomplish, posted his code that has a bug, but is close (ish) to working. Keep at it Jeff, hopefully @Stephen/my hints can help you figure it out, if not let us know. – jb. Nov 06 '15 at 00:26
  • Well some of the down-votes were probably an (understandable) reaction to an offensive comment that the OP made. – Stephen C Nov 06 '15 at 01:44
  • @StephenC lol no I had 2 downvotes before that. Thank you guys for the help also. – Jeff Teague Nov 06 '15 at 01:54
  • Hint: you are (potentially) comparing all characters of `s2` against `s1[i]`. That's not right ... is it. – Stephen C Nov 05 '15 at 23:59
  • Well I would think it is because I am checking if s1 is in s2 – Jeff Teague Nov 06 '15 at 00:01
  • Sorry ... I wasn't accurate in what I wrote ... read it again. And no, it isn't. – Stephen C Nov 06 '15 at 00:04
  • It makes sense to me that I compare s1[i] to all of s2 until I find a match? – Jeff Teague Nov 06 '15 at 00:14

3 Answers3

1

Let's walk through it

s1 = boat
s2 = steamboat
i = 0
j = 0

//walking through the code:

if(b == s) // nope, increment j (j now equals 1), begin next loop iteration

if(b == t) // nope, repeat

if(b == e) // nope, repeat until...

if(b == b) // oh, this looks good! what do we need to do now?

if(b == o) //j got incremented again, doh!
jb.
  • 9,921
  • 12
  • 54
  • 90
  • Ok I understand I need to stop the for loop once I find a match. but how? we haven't even learned this in class yet. – Jeff Teague Nov 06 '15 at 00:32
  • My guess would be to put another for loop inside of my if statement to see if the rest equals s1 – Jeff Teague Nov 06 '15 at 00:34
  • You're on the right track. It helps if you work it out on paper first. then figure out how the code would look. Find the letter 'b' (the first letter in your smaller string) in the bigger string, if you can't find it at all, good, it's not a substring. if you do find it, see if the very next letter in the two strings match. repeat. – jb. Nov 06 '15 at 00:37
  • I updated my script. could you please look at it and see if im even close. Im not getting what I want. – Jeff Teague Nov 06 '15 at 01:04
  • Forget the code for now, just figure it out on paper first. What rules do you need to follow? ie, have a "pointer" point at a single letter in each string. if the letters are equal what needs to happen? (move both pointers forward) if they are not equal, what happens? (move the big pointer forward, reset little pointer). when do we stop moving pointers? – jb. Nov 06 '15 at 01:38
1

I imagine two ways to do this. The first one builds on your approach.

boolean containmentCheck(String big, String small) {
    boolean contained;
        try {
            for (int i = 0; i < big.length(); i++) {
                contained = big.charAt(i) == small.charAt(0);
                if (contained) {
                    for (int j = 1; j < small.length(); j++) {
                        contained = big.charAt(i + j) == small.charAt(j);
                        if (!contained) {
                            i += j;
                            break;
                        }
                        if (j == small.length() - 1)
                            return contained;
                    }
                }
            }
            if (big.length() == 0 && small.length() == 0)
                contained = true;
        } catch (IndexOutOfBoundsException e) {
            contained = true;
        }
    return contained;
}

The second is radically different, but I think you'll find it's much more simple.

boolean containmentCheck(String big, String small) {
    return big.contains(small);
}

The lesson to be learned here is: Read the API very carefully.

G. Ekemark
  • 46
  • 4
  • Thanks this would work for me only if I could use .contains(); . and we havent learned break; yet. – Jeff Teague Nov 06 '15 at 01:03
  • @JeffTeague Well, you have learned it now. `break` will exit `while`, `for` and `do` loops in advance. It should be said that there is any number of ways this could be done. – G. Ekemark Nov 06 '15 at 01:22
1

One more approach. Basically its the same as Ekemark. You should know what 'Continue' is.

boolean isStringSubString(String subStr, String mainStr) {
boolean isSubString = false;
//This is important. We need to loop only the difference of length times.
int max = mainStr.length() - subStr.length();

outerLoop : for (int i = 0; i <= max; i++) {      
  int n = subStr.length();
  int j = i;
  int k = 0;
  while (n != 0) {
    if (mainStr.charAt(j++) != subStr.charAt(k++)) {
      continue outerLoop;
    }
    n--;
  }
  isSubString = true;
  break outerLoop;
}
return isSubString;

}

shan
  • 288
  • 4
  • 11