练习4-1 编写一个函数strrindex(s, t),用于返回字符串t在s中最右出现的位置,如果 s中不 包含t,那么返回-1。

#include <stdio.h>
#include <string.h>
int strrindex (char s[] , char t[] ){
    int i , j , k;
    int len = strlen ( s ) ;
    for ( i = len - 1 ; i >= 0 ; i--){
        for ( j = i , k = 0 ; t[k] != 0 && s[j] == t[k] ; j++ , k++ )
            ;
        if ( k > 0 && t[k] == '\0') return i;
    }
    return -1;
}

int main(){
    char t[]="rld";
    char s[]="Hello,world!Do you love the world.YES,I love!";
    int pos;
    pos = strrindex ( s , t ) ;
    printf("%d",pos);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44127727/article/details/88096368
今日推荐