Arpit Agrawal
Arpit Agrawal

Reputation: 49

Code working on DEvC++ and Ideone but not on competetion website

This code works on DEVCPP and IDEONE but is not accepted by the website where this question is. It is an on-line competition where the in built compiler is saying: COMPILATION FAILED. This is a program which finds str2 in str1 and returns index of sub-string if found. Else prints -1.

I know using GOTO is not recommended. :( Sorry for this. Cant figure out a way for this.

IDEONE link: LINK

Code is:

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

int main()
{
    char s1[19], s2[19],*p,c,d,k=0;
    int i;
    gets(s1);   gets(s2);
    p = strstr(s1,s2);

    if( (strlen(s2) > strlen(s1))   ||   !p      ) 
    {printf("-1");goto ex;}

    for(i=0;i<strlen(s1); i++)
    {
        c = s1[i];
        d = s1[i+1];
        if(c == s2[k]      &&    d == s2[k+1])
        {
            printf("%d", i);
            goto ex;

        }
    }
    ex:
    return 0;
}

Upvotes: 1

Views: 89

Answers (1)

Bernd Elkemann
Bernd Elkemann

Reputation: 23560

a.c:15:14: error: comparison between signed and unsigned integer expressions

Make i unsigned.

a.c:19:9: error: array subscript has type 'char'

Make k int.

Notes:

Use fgets instead of gets.

goto is ok in some circumstances, like the pattern you use can be used to do some default-stuff (eg logging) before return. Also for dropping out from inner loops, jump-table usage (&&), doing default: after having done one of the case x:'s, retry after case x: and a few others.

Upvotes: 1

Related Questions