José Salgado
José Salgado

Reputation: 119

C function to compare last elements of a string to another using pointers

My problem is to see if str1 ends with the contents of str2.

I tried this:

int string_ends_with(const char *str1, const char *str2) {
    int len;
    len = strlen(str2);

    while ((*str1 == '\0' - len) && *str1 != '\0') {
        if (strcmp(str1, str2) == 0) {
            return 1;
        } else {
            return 0;
        }
    }
}

int main() {
    char str[10] = "banana";
    char str1[5] = "ana";

    string_ends_with(str, str1);

    return 0;
}

What I am doing wrong?

This problem needs to be solved only using pointers.

Upvotes: 4

Views: 115

Answers (3)

chqrlie
chqrlie

Reputation: 144949

You initial test is incorrect, as noted in the other answers.

Here is a slightly smaller and potentially more efficient version:

int string_ends_with(const char *str1, const char *str2) {
    const char *p1 = str1;
    const char *p2 = str2;

    while (*p1) p1++;  // same as p1 += strlen(p1);
    while (*p2) p2++;  // same as p2 += strlen(p2);

    while (p2 != str2) {
        if (p1 == str1 || *--p1 != *--p2)
            return 0;
    }
    return 1;
}

Note that your main test function should output something and perform more tests:

void test(const char *a, const char *b) {
    printf("\"%s\" ends with \"%s\": %s\n", 
           a, b, string_ends_with(a, b) ? "yes" : "no");
}

int main(void) {
    char str[] = "banana";
    char str1[] = "ana";

    test(str, str1);
    test(str1, str);
    test(str, str);
    test(str, "");
    test("", str);
    test("", "");

    return 0;
}

Upvotes: 0

Nunchy
Nunchy

Reputation: 948

This will be useful if you want to know which string has a greater/lesser value.

int string_ends_with(char *p1, char *p2)
{
    char    *q1;
    char    *q2;

    if (! p1 || ! p2)
        return something;

    (p1 + (strlen(p1) - 1));
    (p2 + (strlen(p2) - 1));

    while (q1 >= p1 && q2 >= p2) {
       if (*q2 < *q1) return -1;
       else if (*q2 > *q1) return 1;

       q1--; q2--;
    }

    return 0;
}

EDIT: Of course do basic error checking, i'd presume that's a given for most and prefer not to clutter my meagre offering with un-necessary code with regards to a simple example. Still, don't want to ruffle any feathers.

Alright.

Upvotes: 0

Vlad from Moscow
Vlad from Moscow

Reputation: 311058

This condition in the while statement

while((*str1 == '\0' - len) && *str1 != '\0')

does not make sense. What does '\0' - len mean ?

You could write the function the following way

int string_ends_with(const char *str1, const char *str2)
{
    size_t n1 = strlen( str1 );
    size_t n2 = strlen( str2 );

    return n2 <= n1 && strcmp( str1 + n1 - n2, str2 ) == 0;
}

If you may not use standard functions then you can write the function the following way

int string_ends_with(const char *str1, const char *str2)
{
    const char *p1 = str1;
    const char *p2 = str2;

    while ( *p1 ) ++p1;
    while ( *p2 ) ++p2;

    while ( p1 != str1 && p2 != str2 && *p1 == *p2 ) --p1, --p2;

    return  p2 == str2 && *p1 == *p2;
}

Upvotes: 5

Related Questions