Write code to check if a string contains a substring.
Click for Solution
  • A #include <stdio.h>

    int compare(char *p, char *s)
    {
    char *prev,*beg;

    while(*p)
    {
    if(*p==*s)
    {
    prev=p;
    beg=s;
    while(*s)
    {
    if(*p!=*s)
    {
    p=prev;
    s=beg;
    break;
    }
    else
    {
    p++;
    s++;
    }
    }

    if(*s=='\0')
    return 1;
    }
    p++;
    }
    return 0;
    }

    void main()
    {
    int y;
    char *p,*s;
    char a[20]="Raktimktir";
    char b[20]="ktir";

    p=a;
    s=b;

    y=compare(a,b);
    if(y==0)
    printf("NOT FOUND\n");
    else
    printf("FOUND\n");

    }

[Insert Code]