在字符串s中查寻子串t最后一次出现的位置

在字符串s中查找子串t最后一次出现的位置
/*
 * 设计一个算法,在字符串s中查找子串t最后一次出现的位置
 */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int lastindexof(char *s,char *t)
{
	char *s1=s;
	char *t1=t;
	int n1=strlen(s1);
	int n2=strlen(t1);
	int i=0;
	int j=0;
	//找到:true 没找到:false
	bool find=false;
	int loc=0;
	while(i<n1&&j<n2)
	{
		//存在继续比较的意义
		if(*(s1+i)==*(t1+j))
		{
			char *s2=s1+i+1;
			char *t2=t1+j+1;
			while(*s2!='\0'&&*t2!='\0')
			{
				if(*s2==*t2)
				{
					s2++;
					t2++;
				}
				else
				{
					break;
				}
			}
			//找到
			if(*t2=='\0')
			{
				find=true;
				loc=i;
			}
			i++;
		}
		//不存在继续比较的意义s
		else
		{
			i++;
		}
	}
	if(find)return loc;
	else return -1;
}

int main()
{
	char *s="cdababcefabc";
	char *t="abc";
	int loc=lastindexof(s,t);
	printf("最后一次出现的位置:%d\n",loc);
	return 0;
}