Naive String Search Algorithm

Naive String Search is string searching algorithm. It is a simple but inefficient way to see where one string occurs inside another is to check each place it could be, one by one, to see if it's there.



									public static int[] SearchString(string str, string pat)
{
	List<int> retVal = new List<int>();
	int M = pat.Length;
	int N = str.Length;

	for (int i = 0; i <= N - M; i++)
	{
		int j;

		for (j = 0; j < M; j++)
		{
			if (str[i + j] != pat[j])
				break;
		}

		if (j == M)
			retVal.Add(i);
	}

	return retVal.ToArray();
}
								


Example

									string data = "the quick brown fox jumps over the lazy dog";
int[] value = SearchString(data, "the");
								


Output

									0
31