search

Home  >  Q&A  >  body text

c++ - C语言用if(strstr(s1,s2))判断字符串是否存在子字符串,无论有没有都会进入到if里是怎么回事?

代码作用是首先得到5行数字字符串
然后得到一个想要查找的重复数字型子字符串的长度和数字,比如长度3,数字0,就是查找'000'
然后判断第几行有子字符串,打印出来
我现在的问题是……不管有没有,返回结果都是有
还有最后一行的%s没有办法把数组字符串打印出来,我怀疑那个我整合的子字符串有问题,但是不太懂,求指点

int main(){
    char r1[5],r2[5],r3[5],r4[5],r5[5];
    int num=0,sn[10];
    char tf[10];
    int n,s;
    gets(r1);
    gets(r2);
    gets(r3);
    gets(r4);
    gets(r5);
    printf("Length of the sequence =");
    scanf("%d",&n);
    printf("Value of the sequence =");
    scanf("%d",&s);
    for (int i=0; i<n; i++) {
        tf[i]=s;
    }
    for (int i=0; i<n; i++) {
        printf("%d\n",tf[i]);
    }
    if(strstr(r1,tf)){
        num++;
        sn[num-1]=1;
    }
    if(strstr(r2,tf)){
        num++;
        sn[num-1]=2;
    }
    if(strstr(r3,tf)){
        num++;
        sn[num-1]=3;
    }
    if(strstr(r4,tf)){
        num++;
        sn[num-1]=4;
    }
    if(strstr(r5,tf)){
        num++;
        sn[num-1]=5;
    }
    printf("%d\n",num);
    for (int i=0; i<num; i++) {
        printf("在第%d行发现了%s\n",sn[i],tf);
    }
}
怪我咯怪我咯2778 days ago988

reply all(2)I'll reply

  • PHPz

    PHPz2017-04-17 13:14:53

    The assic code value of 0 should be NUL.
    It should be tf[i] = (char) (s + 48);

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 13:14:53

    If it does not exist, a Null pointer is returned

    reply
    0
  • Cancelreply