Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

How to debug this substring matching code?

I am a beginner in C programming, and this is a string matching code I wrote. It aims to find the positions where the substring t appears in the string s and print them. The use of pointers is required.This code scored 94 out of 100 in the OJ test.

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

int main () {
    char *s = malloc(100005);
    char *t = malloc(100005);

    scanf ("%s%s", s, t);

    char *ptrS = s;
    char *ptrT = t;

    if (s == NULL) {
        free(t);
        return 1;
    }
    if (t == NULL) {
        free(s);
        return 1;
    }

    while ( *ptrS != '\0') {
        if (*ptrT == *ptrS) {
            ptrT++;
            ptrS++;
        } else if (*ptrS != *(ptrT = t)) {
            ptrS++;
        }

        if (*ptrT == '\0') {
            printf("%d ", (ptrS - s) - (ptrT - t));
            ptrS = ptrS - (ptrT - t) + 1;
            ptrT = t;
        }
    }

    free(t);
    free(s);

    return 0;
}

I have tried many test cases that I could think of, and it gives the correct results for all of them. I hope to find any bugs or any test cases that cause it to error.

Answer*

Cancel
6
  • That shall not work. If *ptrT is not equal to *ptrS then you shall not change the pointer ptrS if *ptrT is equal to '\0'. Otherwise the output will be incorrect. See my answer. Pay attention to that you should not write such answers. It could be a comment. Commented Nov 26, 2024 at 16:33
  • @VladfromMoscow, sorry, I'm not so familiar of rules here. But I suppose the code actually works here. The case you mention that" if *ptrT is != *ptrS, and if *ptrT == '\0', the pointer ptrS should not move" will not exist. Because when it comes to the cause if(*ptrT == '0'), the last *ptrT must be equal to the last *ptr, they will not come to the cause 'else if'. In another word, when *ptrT != *ptrS, *ptrT cannot be '\0', because the work we do in the last run of the loop ensures it.
    – Elliott T
    Commented Nov 26, 2024 at 17:25
  • It's a little bit confusing here. But now that both of the codes in the two answers score 100 out of 100 in the Online Judgement test, they might be equivalent. I' ve tested them on my own computer too.
    – Elliott T
    Commented Nov 26, 2024 at 17:35
  • It seems you are right.. The problem is that your code difficult to read. In my opinion the approach I showed in my answer with the for loop is more readable and clear. Commented Nov 26, 2024 at 17:37
  • @VladfromMoscow, That's true, my original code was indeed hard to read. As in your answer, iterating through s to match t is a very natural approach. I can't quite explain why I came up with such kind of strange method in the original code; perhaps it's because I'm not yet very familiar with pointers. This code is an assignment from my first time learning pointers.
    – Elliott T
    Commented Nov 26, 2024 at 17:47