1

I want to check if string b is suffix of string a. I have tried this so far:

    char a[20], b[20];
    char *p;
    gets(a);
    gets(b);

    p = strstr(a,b);
    while(p != NULL)
    {
        if(p + strlen(b) == '\0')
            break;
        p = strstr(p+1, b);
    }

I have opened the debugger and have seen that when the program reaches this line:

if(p + strlen(b) == '\0')

It never validates to true because p + strlen(b) isn't \0 but just \.

How can I add \0 at the end of what p is pointing to?

4

1 回答 1

1

您需要了解您正在计算的指针:

任何一个

if(*(p + strlen(b)) == '\0')

或者

if(p[strlen(b)] == '\0')

应该这样做。

于 2014-02-07T21:50:50.690 回答