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?

1 Answer 1

1

You need to derfernce the pointer that you are computing:

Either

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

or

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

should do it.

Sign up to request clarification or add additional context in comments.

2 Comments

Which library should I use for strrstr? Cstring or String.h doesn't seem to work for me.
You are right. It is a non-standard function available on linux via sstrings2.h, and possibly having to link using -lsstrings2: linux.die.net/man/3/strrstr. I have removed it from my answer because of this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.