其他分享
首页 > 其他分享> > 查找字符串

查找字符串

作者:互联网

#include <string.h>
#include <stdio.h>
#define MAXS 30

void ReadString( char s[] )
{gets(s);}

int main()
{
    char s[MAXS], t[MAXS], *pos;

    ReadString(s);
    ReadString(t);
    pos = search(s, t);
    if ( pos != NULL )
        printf("%d\n", pos - s);
    else
        printf("-1\n");

    return 0;
}
char *search(char *s, char *t){
	char *p=NULL;
	int i,j,k=0,lens,lent;
	
	lens = strlen(s);
	lent = strlen(t);
 
	for(i=0;i<lens;i++){
		j=i;
		while(s[j]==t[k]){
			k++;
			j++;
		}
		if(k>=lent){  // k>=lent 才能满足   长度超过题面MAXS, t在结尾处  这个条件 
			p=&s[i];
			return p;
		}
		k=0;
	}
 
	return p;
}

 

标签:return,MAXS,pos,char,查找,lent,字符串,ReadString
来源: https://blog.csdn.net/qq_43677393/article/details/90316942