练习5-4 编写函数strend (s, t): 如果字符串t出现在字符串s的尾部,则返回1;否则返回0

/* 编写函数strend (s, t): 如果字符串t出现在字符串s的尾部,则返回1;否则返回0。 */
#include <stdio.h>
int strend(char *s , char *t ){
    char *a = s;
    char *b = t;//记录两个指针的初始位置

    while ( *s )
        s++;
    while ( *t )
        t++; //记录字符串的结束位置
    for ( ; t >= b && s >= a ; t-- ,s--){
        if ( *t != *s )//如果t指向的字符 和 s指向的字符不同
            return 0;
    }
    return 1;
}
int main ()
{
    char a[20] = "Hello world!";
    char b[10] = "Hello";
    char *s = a;
    char *t = b;
    int result = strend( s , t );
    if ( result )
        printf("是");
    else printf("否");
    return 0;
}

猜你喜欢

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