自己用 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;
}