PTA: Qiuzi string appears rightmost position (15 minutes) (C language)

Interface definition function:
int strrindex (S char [], char P []);

Wherein p and s are parameters passed user. The length of the string s and p does not exceed 100. P shall function returns the string that appears rightmost in s (starting with index 0). If s does not contain p, -1 is returned.

Referee test sample program:
#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));
}

/ * Please fill in the answer here * /

Sample Input 1:
ABCDE cde

Output Sample 1:
2

Sample Input 2:
ABCDE FGH

Output Sample 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;
}
Published 58 original articles · won praise 21 · views 606

Guess you like

Origin blog.csdn.net/qq_45624989/article/details/105399556