I want to write a method boolean subString()to judge if a string is a s1substring s2.
The requirement is only to be used charAt() and length() methods String .
eg.
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;
}
}
Testing Class:
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")because it returns true.
source
share