编写一个使用指针返回类型的函数,使用该函数在字符串中搜索一个子串,并返回第一个相匹配的子串指针

来源:百度知道 编辑:UC知道 时间:2024/09/22 02:03:39
编写一个使用指针返回类型的函数,使用该函数在字符串中搜索一个子串,并返回第一个相匹配的子串指针。该函数的原型如下:
char * GetSubstr(char *sub, char *str);
测试程序:
#include <iostream>
using namespace std;

char * GetSubstr(char *sub, char *str)
{
//...请补充完整
}

int main()
{
char *substr;
substr=GetSubstr("two","One two three four five");
cout < <"找到的子串为:" < <substr < <endl;
return 0;
}

程序运行结果为:
找到的子串为:two three four five

这个函数怎么写呢?谢谢

#include <iostream>
using namespace std;

char * GetSubstr(char *sub, char *str)
{
char *tem;
tem=str;
while(*tem!=*sub)
{
tem++;
}
return tem;
}

int main()
{
char *substr;
substr=GetSubstr("two","One two three four five");
cout<<"找到的子串为:"<<substr<<endl;
return 0;
}