0

I am trying to check if a string is a sub-string of another string. I have wrote my code so that the big string is compared with the smaller strings, so that i can find out if the smaller strings are sub-strings of the big string. But when i try to run my code, its acting wrong and not taking input as it's supposed to.i think it might be printf problem. But Don't know what the actual problem is. Any help is highly appreciated.

Here's a link to the problem i am trying to solve: https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=18&page=show_problem&problem=1620


#include<stdio.h>
#include<string.h>
int main()
{
    int k,i,j,l,m;
    scanf("%d ",&k);
    for(i = 1; i <= k; i++)
    {
        char S[100001];
        int q;
        fgets(S,100000,stdin);
        scanf("%d",&q);
        char T[q][1000];
        for(j = 0; j < q; j++)
        {
            fgets(T[j],999,stdin);
            for(m = 0,l = 0; m < strlen(T[j]);)
            {
                if(l >= strlen(S))
                {
                    printf("n\n");
                    fflush(stdout);
                    break;
                }
                if(S[l] == T[j][m])
                {
                    if(m == (strlen(T[j]) - 1))
                    {
                        printf("y\n");
                        fflush(stdout);
                        break;
                    }
                    m++;
                    l++;
                }
                else
                {
                    l++;
                    m = 0;
                }
            }
        }


    }
    return 0;
}

  • 2
    You shouldn't use `gets` https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used – anastaciu Apr 22 '20 at 15:48
  • Then what should i use? – cryomancer148 Apr 22 '20 at 15:49
  • 1
    There are some alternatives, the link I provided covers this, you can use `fgets`, note that this is not directly related to the problem you lay out in the question but is something that is important to know. – anastaciu Apr 22 '20 at 15:52
  • I don't know what compiler you use, but as far as I know all of them emit warnings along the lines of `prog.c:(.text+0x64): warning: the gets function is dangerous and should not be used.` – anastaciu Apr 22 '20 at 15:55
  • I am using codeblocks – cryomancer148 Apr 22 '20 at 15:59
  • I have updated the code with fgets but still getting unexpected printf outputs – cryomancer148 Apr 22 '20 at 16:03
  • What's the program input and output? – Oppen Apr 22 '20 at 16:05
  • `scanf` read the number but not the ending newline character (`\n`). Have you tried to add `getchar();` after the `scanf` calls? – Roy Avidan Apr 22 '20 at 16:05
  • No. But i have added a space after %d in scanf. Might try getchar then – cryomancer148 Apr 22 '20 at 16:06
  • Ok. Using getchar solved it. But now i am facing another problem. Only small strings that are exactly like the big strings show that those small strings are sub strings. If "runn" is the big string, only small string "runn" shows its a sub string of the big string. But other small strings like "r","ru" ,"run"which are definitely sub-strings of the big string "runn", show that those strings are not the sub-strings of "runn" – cryomancer148 Apr 22 '20 at 16:14
  • Is this new problem created due to fgets? I don't know how to use fgets property – cryomancer148 Apr 22 '20 at 16:15
  • Yeah your substring search is broken. Why not use `strstr` instead? – Zan Lynx Apr 22 '20 at 16:45
  • Ok, i'll give it a try. – cryomancer148 Apr 22 '20 at 17:36
  • Ok. I used gets instead of fgets and it's working perfectly now. And also thx for `strstr` function. I didn't know a function existed just for finding sub-strings – cryomancer148 Apr 22 '20 at 17:48

0 Answers0