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.



									/*****Please include following header files*****/
// string
// vector
/***********************************************/

/*****Please use following namespaces*****/
// std
/*****************************************/

static vector<int> SearchString(string str, string pat) {
	vector<int> retVal;
	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.push_back(i);
	}

	return retVal;
}
								


Example

									string data = "the quick brown fox jumps over the lazy dog";
vector<int> value = SearchString(data, "the");
								


Output

									0
31