Determine whether string2 occurs with string1 or not
Solution 1:
Your else if(count==l2)
statement is in a wrong place. In your code, this condition does not check when ch1 == ch2
. The following may help you:
public boolean occurs(String str1, String str2) {
int l1=str1.length();
int l2=str2.length();
int p=0;
int count=0;
int j=0;
for(;j<l1;j++) {
char ch1=str1.charAt(j);
char ch2=str2.charAt(p);
if(ch1==ch2) {
p++;
count++;
} else {
p=0;
count=0;
}
if(count==l2) {
break;
}
}
if(l2==count)
return true;
else
return false;
}