/* **在字符串s1中查找字符串s2最右出现的位置, **并返回一个指向该位置的指针 */ #include< string.h > char * my_strrstr( char const *s1, char const *s2 ) { register char *last; register char *current; /* **把指针初始化为我们已经找到的前一次匹配置。 */ last = NULL; /* **只在s2不为空时才进行查找,如果s2为空,返回NULL。 */ if( *s2 != '\0' ) { /* **查找s2在s1中第一次出现的位置。 */ current = strstr( s1, s2 ); /* **我们每次找到字符串时,让指针指向它的起始位置, **然后 查找该字符串的下一个匹配位置。 */ while( current != NULL ) { last = current; current = strstr( last + 1, s2 ); } } /* 返回指向我们找到的最后一次匹配的起始位置的指针 */ return last; }