strstr函数

来源:互联网 发布:太仓港数据信息中心 编辑:程序博客网 时间:2024/05/16 19:52

 

////返回包括子串的整个字符串

 

#include<string.h>
#include<stdio.h>
#include<iostream>

using namespace std;

const char* strstr1(const char *s,const char* str)
{
 for(int i =0;s[i]!='/0';i++)
 {
  int j = 0;
  if(s[i]==str[j])
  {
   while(s[i++]==str[j++])
   {   //字串到头了
    if((str[j]=='/0')) 
     return &s[i-j];
   }
  }
 }
 return 0;
}

void main()
{
 char * s="012345678901234567890123456789";
 printf("s = %s/n",s);
 char str[10]={0};
 cin>>str;
 cout<<strstr1(s,str)<<endl;
 
 //p= strstr(s,"901");  ////返回包括字串后面的整个字符串
 //printf("%s/n",p);
}