Search code examples
calgorithmfor-loopsubstringc-strings

is there a reason why my isSubstring algorithm from GeeksforGeeks isn't working


Is there a reason why my isSubstring algorithm is not working? It's the same algorithm provided here: https://www.geeksforgeeks.org/check-string-substring-another/ but keeps returning 2 even though they it is the same substring in my eyes.

int main()
{
    char substring[] = "New York";
    char stringVal[] = "Joshua,New York,Engineer";
    int M = sizeof(substring);
    int N = sizeof(stringVal);
    #pragma clang loop unroll(full)
    for (int i = 0; i <= N - M; i++) {
        int j;

        for (j = 0; j < M; j++) {
            if (stringVal[i + j] != substring[j]){
                break;
            }
        }
 
        if (j == M) {
            printf("%d", 1);
            return 1;
        }
    }
    printf("%d", 2);
    return -1;
}

Solution

  • These variables

    int M = sizeof(substring);
    int N = sizeof(stringVal);
    

    count also terminating zeroes of the strings.

    Thus in this for loop

        for (j = 0; j < M; j++) {
            if (stringVal[i + j] != substring[j]){
                break;
            }
        }
    

    there will be compared the terminating zero '\0' of the string substring with a non-terminating zero character of the string stringVal.

    You need to exclude the terminating zeroes for example like

    int M = sizeof(substring) - 1;
    int N = sizeof(stringVal) - 1;
    

    Or as Paul Sanders pointed out it is even better to use the standard C function strlen.

    #include <string.h>
    
    //...
    
    int M = strlen(substring);
    int N = strlen(stringVal);
    

    And instead of the type int you should use the type size_t

    #include <string.h>
    
    //...
    
    size_t M = strlen(substring);
    size_t N = strlen(stringVal);
    

    The type size_t is the return type of the function strlen and the type of the value of the operator sizeof.