void CTestTmp123Dlg::OnButton1()
{
char charar1[] = "I have an apple, aaa, aabb, abb", charar2[] = "abb", *p;
int intPosition;
// 下面四行用C的库函数,没找到返回-1
if (p = strstr(charar1, charar2))
intPosition = (p - charar1);
else
intPosition = -1;
// 下面四行用VC++的库函数,没找到返回-1
intPosition = CString(charar1).Find(charar2);
// 下面是自己写的不用任何库函数的方法,没找到返回-1
intPosition = StrStr(charar1, charar2);
}
int CTestTmp123Dlg::StrStr(char charar1[], char charar2[])
{
int intCheckStart, intState;
for (intCheckStart = 0; intCheckStart < strlen(charar1)-strlen(charar2); intCheckStart++)
{
intState = 0;
while (charar1[intCheckStart+intState] == charar2[intState] && intState < strlen(charar2))
intState++;
if (intState == strlen(charar2)) // find the match
return intCheckStart;
}
return -1;
}