//模拟一个C++的函数strstr()函数:该函数的返回值是主串中字符串子串的位置以后的所有字符
//例如主串是12345678,子串是234,那么函数返回值是2345678
#include <iostream>
using namespace std;
const char* strstr1(const char* string ,const char* strCharSet)
{
for(int i=0; string[i] != '\0'; i++)
{
int j=0;
int temp=i;
if (string[i] == strCharSet[j])
{
while(string[i++] == strCharSet[j++])
{
if((strCharSet[j]) =='\0')
return &string[i-j];
}
i=temp;
}
}
return NULL;
}
int main()
{
char* string = "12345554555123";
cout << string <<endl;
char strCharSet[10] ;
cin >> strCharSet;
cout << strstr1(string,strCharSet) <<endl;
return 0;
}
//例如主串是12345678,子串是234,那么函数返回值是2345678
#include <iostream>
using namespace std;
const char* strstr1(const char* string ,const char* strCharSet)
{
for(int i=0; string[i] != '\0'; i++)
{
int j=0;
int temp=i;
if (string[i] == strCharSet[j])
{
while(string[i++] == strCharSet[j++])
{
if((strCharSet[j]) =='\0')
return &string[i-j];
}
i=temp;
}
}
return NULL;
}
int main()
{
char* string = "12345554555123";
cout << string <<endl;
char strCharSet[10] ;
cin >> strCharSet;
cout << strstr1(string,strCharSet) <<endl;
return 0;
}
本文介绍了一个用 C++ 实现的 strstr() 函数模拟版本,该函数用于查找一个字符串在另一个字符串中首次出现的位置,并返回从该位置开始的剩余字符串。文章通过示例演示了如何使用此函数。

被折叠的 条评论
为什么被折叠?



