This is my code that i have written, it is giving wrong output.I have to print the index where the pattern is found. Like if string = "ABCABCDEABCDEA" and pattern = "ABCD", the output will be 4 and 9
Let me discuss it's approach-What i want is if the ith element of the string is equal to the 0th element of the pattern. then go inside the loop, and check till pattern.length. Else continue.
public class Word_check {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str1="ABCDAFGFGFABCDKLHKHABCD";
String tocheck="ABCD";
boolean isfound=false;
int count=0;
for(int i=0;i<str1.length();i++) {
if(str1.charAt(i)==tocheck.charAt(count)){
for(int j=i;j<tocheck.length();j++) {
if(str1.charAt(j)==tocheck.charAt(count)) {
isfound=true;
}
else {
isfound=false;
break;
}
count++;
}
if(isfound==true) {
System.out.println(i+1);
}
}
else {
continue;
}
count=0;
}
}
}