bslqy
bslqy

Reputation: 233

How to use charAt() and length() to write a whether is substring method

I want to write a boolean method subString() to judge if string s1 is the substring of s2.

The requirement is only to use charAt() and length() methods of String.

E.g.

 Substring("abc","abcd")-> true

 Substring("at","cat")->true

 Substring("ac","abcd")->false

indexOf() cannot be used.

Here is what I got so far.

public class Q3 {
    public boolean subString(String str1, String str2) {
        String s1 = str1.toLowerCase();
        String s2 = str2.toLowerCase();
        for (i = 0; i < s1.length; i++) {
            for (j = 0; j < s2.length; j++) {
                if (s1.charAt(i) == s2.charAt(j))
                    return true;
            }
        }
        return false;
    }
}

Test class is :

public class Q3test {
    public static void main (String arg[]){
        Q3 Q3object = new Q3();
        System.out.println(Q3object.Substring("ac","abcd"));
    }
}

It fails subString("ac","abcd") as it returns true.

Upvotes: 4

Views: 2279

Answers (3)

raymelfrancisco
raymelfrancisco

Reputation: 871

You should also check if the characters are in order, and are in-next to each other. Your code only checks for the existence of characters.

public static boolean subString(String a, String b) {

    int checker = 0;
    String sub = a.toLowerCase();
    String supr = b.toLowerCase();

    // loops through all the characters of superstring
    for (int sp = 0; sp < supr.length(); sp++) {

        // finds the first character of the substring
        if (supr.charAt(sp) == sub.charAt(0)) {

            // loops through the characters of the substring
            for (int sb = 0, tmp = sp; sb < sub.length(); sb++, tmp++) {
                if (supr.charAt(tmp) == sub.charAt(sb))
                    checker++; // increments checker for every char match
            }
            // resets checker if not all characters for sub is equal to super based on their order
            if (checker != sub.length())
                checker = 0;
        }

        // match occurred if all characters of the substring is present in the superstring, 
        // in which they are in the same order, and all are existing consecutively
        if (checker == sub.length())
            return true;
    }
    return false;
}

Let's say we have:

public static void main(String[] args) {
    System.out.println(subString("abc", "abcd"));
    System.out.println(subString("at", "cat"));
    System.out.println(subString("ac", "abcd"));
}

The output will be:

true
true
false

Upvotes: 0

Eran
Eran

Reputation: 393936

Your code returns true if the first character matches. You need all the characters of the first String to be contained in a substring of the second String.

EDIT:

My original code was wrong. Here's the correct code :

        public static boolean subString(String str1, String str2)
        {
          String s1 = str1.toLowerCase();
          String s2 = str2.toLowerCase();
          for (int offset = 0; offset <= s2.length() - s1.length(); offset++) {
            int i = 0;
            for (; i < s1.length(); i++){
              if(s1.charAt(i) != s2.charAt(i+offset)) {
                break;
              }
            }
            // found a substring that starts at the current offset
            if (i == s1.length())
              return true;
          }
          return false;
        }

Upvotes: 4

Raheel138
Raheel138

Reputation: 147

The contains method of the string class should do

For examples please refer to this: http://www.tutorialspoint.com/java/lang/string_contains.htm

Upvotes: 0

Related Questions