Skip to main content
edited body
Source Link
SylvainD
  • 29.8k
  • 1
  • 49
  • 93

I was about to say that I had found a bug with the following test cases while trying to understand how your logic works :

char *s1 = "an abyssan abyssan abyssan abyss.";
char *s2 = "an abyss.";

char *s1 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.";
char *s2 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaa.";

I reckon you should start by writing test cases and check that your code produces the results you are expecting.

For instance, here is what I have written. I won't say it's bug free because I haven't spent much time on it but at least I have a bit of confidence because it seems to go through the different tests I have written :

#include <stdio.h>
#include <assert.h>

int str_end(const char *s, const char *t)
{
    // Going forward on both strings til we reach an end
    int l;
    for (l=0; *s && *t; s++, t++, l++);

    // If t is not over ... 
    if (*t)
    {
        // ... then s should be
        assert(!*s);
        // so s is shortedshorter and there is no point in going further
        return 0;
    }

    assert(!*t);

    // Going forward on s
    for ( ; *s; s++);

    // Going backward on both -- this could also be done with a normal string comparison function by going back l steps and comparing remaining strings
    for ( ; l>=0; l--, s--, t--)
        if (*s != *t)
            return 0;
    return 1;
}

int main(void)
{
    assert( str_end("Man is a rope stretched over an abyss.", "an abyss."));
    assert(!str_end("Man is a rope stretched over an abyss.", "un abyss."));
    assert( str_end("Man is a rope stretched over an abyss.", ""));
    assert( str_end("", ""));
    assert( str_end(".", "."));
    assert( str_end(".", ""));
    assert(!str_end("", "."));
    assert( str_end("qqqqqqqqqqqqqqqqqqqqqqqqqq", "qq"));
    return 0;
}

I was about to say that I had found a bug with the following test cases while trying to understand how your logic works :

char *s1 = "an abyssan abyssan abyssan abyss.";
char *s2 = "an abyss.";

char *s1 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.";
char *s2 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaa.";

I reckon you should start by writing test cases and check that your code produces the results you are expecting.

For instance, here is what I have written. I won't say it's bug free because I haven't spent much time on it but at least I have a bit of confidence because it seems to go through the different tests I have written :

#include <stdio.h>
#include <assert.h>

int str_end(const char *s, const char *t)
{
    // Going forward on both strings til we reach an end
    int l;
    for (l=0; *s && *t; s++, t++, l++);

    // If t is not over ... 
    if (*t)
    {
        // ... then s should be
        assert(!*s);
        // so s is shorted and there is no point in going further
        return 0;
    }

    assert(!*t);

    // Going forward on s
    for ( ; *s; s++);

    // Going backward on both -- this could also be done with a normal string comparison function by going back l steps and comparing remaining strings
    for ( ; l>=0; l--, s--, t--)
        if (*s != *t)
            return 0;
    return 1;
}

int main(void)
{
    assert( str_end("Man is a rope stretched over an abyss.", "an abyss."));
    assert(!str_end("Man is a rope stretched over an abyss.", "un abyss."));
    assert( str_end("Man is a rope stretched over an abyss.", ""));
    assert( str_end("", ""));
    assert( str_end(".", "."));
    assert( str_end(".", ""));
    assert(!str_end("", "."));
    assert( str_end("qqqqqqqqqqqqqqqqqqqqqqqqqq", "qq"));
    return 0;
}

I was about to say that I had found a bug with the following test cases while trying to understand how your logic works :

char *s1 = "an abyssan abyssan abyssan abyss.";
char *s2 = "an abyss.";

char *s1 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.";
char *s2 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaa.";

I reckon you should start by writing test cases and check that your code produces the results you are expecting.

For instance, here is what I have written. I won't say it's bug free because I haven't spent much time on it but at least I have a bit of confidence because it seems to go through the different tests I have written :

#include <stdio.h>
#include <assert.h>

int str_end(const char *s, const char *t)
{
    // Going forward on both strings til we reach an end
    int l;
    for (l=0; *s && *t; s++, t++, l++);

    // If t is not over ... 
    if (*t)
    {
        // ... then s should be
        assert(!*s);
        // so s is shorter and there is no point in going further
        return 0;
    }

    assert(!*t);

    // Going forward on s
    for ( ; *s; s++);

    // Going backward on both -- this could also be done with a normal string comparison function by going back l steps and comparing remaining strings
    for ( ; l>=0; l--, s--, t--)
        if (*s != *t)
            return 0;
    return 1;
}

int main(void)
{
    assert( str_end("Man is a rope stretched over an abyss.", "an abyss."));
    assert(!str_end("Man is a rope stretched over an abyss.", "un abyss."));
    assert( str_end("Man is a rope stretched over an abyss.", ""));
    assert( str_end("", ""));
    assert( str_end(".", "."));
    assert( str_end(".", ""));
    assert(!str_end("", "."));
    assert( str_end("qqqqqqqqqqqqqqqqqqqqqqqqqq", "qq"));
    return 0;
}
Source Link
SylvainD
  • 29.8k
  • 1
  • 49
  • 93

I was about to say that I had found a bug with the following test cases while trying to understand how your logic works :

char *s1 = "an abyssan abyssan abyssan abyss.";
char *s2 = "an abyss.";

char *s1 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.";
char *s2 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaa.";

I reckon you should start by writing test cases and check that your code produces the results you are expecting.

For instance, here is what I have written. I won't say it's bug free because I haven't spent much time on it but at least I have a bit of confidence because it seems to go through the different tests I have written :

#include <stdio.h>
#include <assert.h>

int str_end(const char *s, const char *t)
{
    // Going forward on both strings til we reach an end
    int l;
    for (l=0; *s && *t; s++, t++, l++);

    // If t is not over ... 
    if (*t)
    {
        // ... then s should be
        assert(!*s);
        // so s is shorted and there is no point in going further
        return 0;
    }

    assert(!*t);

    // Going forward on s
    for ( ; *s; s++);

    // Going backward on both -- this could also be done with a normal string comparison function by going back l steps and comparing remaining strings
    for ( ; l>=0; l--, s--, t--)
        if (*s != *t)
            return 0;
    return 1;
}

int main(void)
{
    assert( str_end("Man is a rope stretched over an abyss.", "an abyss."));
    assert(!str_end("Man is a rope stretched over an abyss.", "un abyss."));
    assert( str_end("Man is a rope stretched over an abyss.", ""));
    assert( str_end("", ""));
    assert( str_end(".", "."));
    assert( str_end(".", ""));
    assert(!str_end("", "."));
    assert( str_end("qqqqqqqqqqqqqqqqqqqqqqqqqq", "qq"));
    return 0;
}