c++编程:编写一函数:int substring (char*str,char *sub),判

2025-03-17 10:52:17
推荐回答(1个)
回答1:

参考代码:

int Index(char s[],char t[],int pos)
{
   int i,j,slen,tlen;
   i = pos;
   j = 0;
   slen = strlen(s);
   tlen = strlen(t);
   while(i    {
        if (s[i] == t[j])
        {
               i++;
               j++;
        }
        else
        {
             i = i-j+1;
             j = 0;
        }
  }
  if(j >= tlen)
            return i-tlen;
            return -1;
}