输入“1234567",字串”234“,返回”2345678“
const char * strstr2(const char * string, char *string2)
{
for (int i = 0; string[i] != ‘\0‘; i++)
{
int j = 0;//每次字串都要从开始位置开始匹配
int temp = i;//记录初始位置
if (string[i] == string2[j])
{
while (string[i++] == string2[j++])
{
if (string2[j] == ‘\0‘)
return &string[i - j];//返回结果
}
}
i = temp;//回溯回去,记住
}
return NULL;
}
int main()
{
char *string = "123456";
char *string2 = "23";
cout << strstr2(string, string2) << endl;
cin.get();
return 0;
}
时间: 2024-10-25 07:01:05