Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
class Solution {
public:
int strStr(string haystack, string needle) {
int i;
int result = 0;
bool flag = false;
int len = needle.length();
if(len==0&&haystack.length()==0)
return 0;
for(i=0; i<haystack.length();i++)
{
if(strcmp(haystack.substr(i,len).c_str(),needle.c_str())==0)
{
flag = true;
break;
}
}
if(flag)
{
return i;
}
else
return -1;
}
};