Following program finds a substring in a string.
public static int Find(string text, string pattern)
{
// ASHI COMPARE A,S,H,I
// SHI A==S,S==H
for (int i = 0; i <=text.Length – pattern.Length; i++)
{
int temp = i;
for (int j = 0; j < pattern.Length; j++)
{
temp++;
if (text[temp] == pattern[j])
{
j++;
temp++;
if (j == pattern.Length)
return 1;
continue;
}
break;
}
}
return -1;
}
Leave a Reply