7个测试用例中的1个不能使用这个简单的C程序来查找子字符串

asr*_*srm 1 c string substring

我已经被赋予编写C程序的任务,以找到包含7个测试用例的子字符串.我编写了程序,但它只传递了6个测试用例.第7次失败,我无法确定其背后的原因.

编辑:这是我给作业的问题.

编写一个程序,它接受两个输入字符串S1和S2,并查找S2是否是S1的子字符串.如果S2是S1的子字符串,程序应该在S1处打印匹配的索引.如果S2不是S1的子字符串,程序应该打印-1.如果S2多次出现在S1中,则在S1中打印匹配发生的第一个索引.

这是我编写的程序的源代码.

#include <stdio.h>
#include <string.h>

int main() {
    char st1[19];
    char st2[19];
    int cnt,i,k,c,len,m,sign;
    scanf("%s %s", st1, st2);
    len=strlen(st1);
    for(i=0; i<len; i++) {
        c=0;
        if (st1[i] == st2[c]) {
            m = i;
            sign = 0;
            cnt = 0;
            while(st2[c] != '\0' && sign!=1) {
                if (st1[m] == st2[c]) {
                    m++;
                    c++;
                    cnt++;
                } else
                    sign=1;
            }
            if (sign == 0) {
                printf("%d",i);
                k=1;
            }
        }
    }
    if (k != 1)
        if (sign!=0)
            printf("-1");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

第7个测试用例如下

输入:

coolgoose oo

预期产量:

1

实际产量:

15

编辑2:以下是其他通过的测试用例.

Input       Output

football foot   0

mickey mouse    -1

abcdefghijklmnopqrs s   18

helloworld helloworld   0

FrodoBaggins bagg   -1

Hell Hello  -1
Run Code Online (Sandbox Code Playgroud)

Mat*_*ert 5

它实际上是打印它找到的每个匹配:字符1和字符5.当你找到匹配时,你应该break离开for循环.