c++ find函数实现
有些地方要用到字符串查找,比如在文件中查找某一字符串位置,又不想使用c++模板库的函数,可以自己写个查找函数
[php]
int find(char* srcBuff, int srcLen, char* findBuff, int findLen, int startPos)
{
int findPos = 0;
int tempPos = 0;
for (int i = startPos; i <= srcLen – findLen; i++)
{
findPos = i;
tempPos = i;
for (int j = 0; j < findLen; j++)
{
char srcTemp = *(srcBuff + tempPos);
char findTemp = *(findBuff + j);
if (srcTemp == findTemp)
{
if (j == findLen – 1)
{
return findPos;
}
tempPos++;
}
else
{
break;
}
}
}
return -1;
}
[/php]