Given two strings str1 and str2 as input, determine whether str2 occurs with str1 or not.
occurs("JavaLadders","Java")=true
occurs("Problem Panel","Panes") = false
Please comment if you find any mistake.
occurs("JavaLadders","Java")=true
occurs("Problem Panel","Panes") = false
public class PatternInString {
static String testcase1 = "May i sit
here";
static String testcase2 = "May";
public static void main(String args[]){
PatternInString
testInstance= new PatternInString();
boolean result =
testInstance.occurs(testcase1,testcase2);
System.out.println(result);
}
//write your code
here
public boolean occurs(String str1,
String str2){
int
lenstr1=str1.length();
int
lenstr2=str2.length();
int j=0;
int i=0;
while(i<lenstr1)
{
if(str1.charAt(i)==str2.charAt(j))
{
int k=i;
while(j<lenstr2
&& k<lenstr1)
{
if(str1.charAt(k)==str2.charAt(j))
{
k++;
j++;
}
else
break;
}
if(j==(lenstr2))
{
return true;
}
else
j=0;
}
i++;
}
return false;
}
}
No comments:
Post a Comment