PTA:求子字符串出现的最右位置 (15分)(C语言)

函数接口定义:
int strrindex(char s[], char p[]);

其中 s 和 p 都是用户传入的参数。字符串 s 和 p 的长度不超过100。函数须返回字符串 p 在 s 中最右边出现的位置(下标从0开始)。如果 s 中不包含 p ,则返回-1。

裁判测试程序样例:
#include <stdio.h>
#include <string.h>

int strrindex(char s[], char p[]);

int main()
{
char s[101], p[101];
scanf("%s %s", s, p);
printf("%d", strrindex(s, p));
}

/* 请在这里填写答案 */

输入样例1:
abcde cde

输出样例1:
2

输入样例2:
abcde fgh

输出样例2:
-1

int strrindex(char s[], char p[])
{
    int i,j;
    int n, k ;
    int judge = 0;
    for ( i = 0; s[i] != '\0'; i++)
    {
        for(k = 0, j = i; s[j] == p[k];j++,k++)
                ;
        if (p[k] == '\0'){
            n = i;
            judge = 1;
        }
            
    }
    if (judge == 1)
        return n;
    else
        return -1;
}
发布了58 篇原创文章 · 获赞 21 · 访问量 606

猜你喜欢

转载自blog.csdn.net/qq_45624989/article/details/105399556