编辑代码

#include <stdio.h>
#include <string.h>
#define MAXLINE 100

//int getline(char line[],int max);
//int strrindex(char *s,char *t);

int getaline(char s[],int lim)
{
	int c,i;

	i = 0;
    /*
	while ( --lim > 0 && ( c = getchar() ) !=EOF && c != '\n' )
		s[i++] = c;
	//if (c == '\n')
	//	s[i++] = c;
	s[i] = '\0';
	return i;
*/
    for(i=0;i<lim;i++)
    {
        c=getchar();//接收输入字符
        if(c!=EOF && c!='\n')   //判断输入是否为回车或结束符
        {
            s[i]=c;//不是则存入字符数组
        }else{//是则存入字符串结束符\0并结束输入
            s[i]='\0';
            printf("输入为:%s\n",s);//测试,显示输入字符串
            break;
        }
    }
    return i;
}

int strrindex (char *s,char *t)
{
	int i,j,k, pos;
	
	pos = -1;
    /*
	for ( i = 0; s[i] != '\0'; i++ ){
		for (j = i,k = 0; t[k] == s[j] && t[k] != '\0'; j++,k++)
			;
		if ( k != 0 && t[k] == '\0' )
			pos =i;
	}
*/
    for(i=0;i<strlen(s);i++)//从第1个字符开始检测s字符串
    {
        for(j=0;j<strlen(t);j++)//用t字符串逐字符对比s字符串对应的字符
        {
            if(s[i+j]!=t[j]){//不相同则结束
                break;
            }
        }
        if(j==strlen(t)){pos=i;}//如果j的值为t字符串长度则意味着
                //t字符串全部对比而且是和s字符串对应部分是相同的,记录对应位置
    }
	return pos;

}


int main()
{
	int l1,l2,pos;
	char line1[MAXLINE],line2[MAXLINE];

	l1 = getaline(line1,MAXLINE);
	l2 = getaline(line2,MAXLINE);

	//printf("%s\n%s\n",line1,line2);

	if ( ( pos =  strrindex(line1,line2) ) >= 0 )
		printf("%d",pos);
	else 
		printf("%d",-1);
    return 0;
}