#include <iostream>
using namespace std;
//a为原字符串,b为子串
//在a串中含有b串,则返回a串从b开始的子串(第一含有b串)
//若a为123423456,b为234,则应返回的子串23423456
char* ReturnSubstring(char *a,char *b)
{
char *p,*q,*temp;//p,q 动态指向a,b,temp指向a中的第一个相同字符
p=a;
q=b;
temp=NULL;
if (NULL==a||NULL==b)
{
cout<<"One of the strings is NULL!"<<endl;
}
while(*p!=NULL)
{
//源串中找到与子串相同的第一个字符
while ((*p!=NULL)&&(*p!=*q))
{
p++;
}
if (NULL==*p)
{
cout<<"没有满足条件的子串!"<<endl;
return NULL;
}
else
{
//用temp暂时保存原串中相同字符的位置
temp=p;
while (*p==*q)
{
p++;
q++;
}
if (NULL==*q)
{
return temp;
}
else
{
p=temp+1;
q=b;
}
}
}
cout<<"没有满足条件的字符串"<<endl;
return NULL;
}
//主函数
int main()
{
char a[]="12323456";
char b[]="7";
if(NULL!=ReturnSubstring(a,b))
{
char *c=ReturnSubstring(a,b);
while(NULL!=*c)
{
cout<<*c;
}
}
return 0;
}