自己用 C 写的一个查找子字符串的函数
int findstr(char *str,char *substr) //C实现 find
{
if(NULL == str || NULL== substr)
return -1;
int strl = strlen(str); //字符串长度
int len = strlen(substr); //要查找的子串的长度
if(len>strl)
return -1;
char *tmp = new char[len+1];
for(int i = 0;i <= (strl-len);i++)
{
strncpy(tmp, str+i, len);
tmp[len] = '\0';
if(0 == strcmp(tmp, substr))
{
delete[] tmp;
tmp = NULL;
return i;
}
}
delete[] tmp;
tmp = NULL;
return -1;
}