#include <assert.h>
#include <string.h>
int string_has_suffix(const char *str, const char *suf)
{
assert(str && suf);
const char *a = str + strlen(str);
const char *b = suf + strlen(suf);
while (a != str && b != suf) {
if (*--a != *--b) break;
}
return b == suf && *a == *b;
}
// Test Unit
int main (int argc, char *argv[])
{
assert( string_has_suffix("", ""));
assert(!string_has_suffix("", "a"));
assert( string_has_suffix("a", ""));
assert( string_has_suffix("a", "a"));
assert(!string_has_suffix("a", "b"));
assert(!string_has_suffix("a", "ba"));
assert( string_has_suffix("abc", "abc"));
assert(!string_has_suffix("abc", "eeabc"));
assert(!string_has_suffix("abc", "xbc"));
assert(!string_has_suffix("abc", "axc"));
assert(!string_has_suffix("abcdef", "abcxef"));
assert(!string_has_suffix("abcdef", "abxxef"));
assert( string_has_suffix("b.a", ""));
assert( string_has_suffix("b.a", "a"));
assert( string_has_suffix("b.a", ".a"));
assert( string_has_suffix("b.a", "b.a"));
assert(!string_has_suffix("b.a", "x"));
assert( string_has_suffix("abc.foo.bar", ""));
assert( string_has_suffix("abc.foo.bar", "r"));
assert( string_has_suffix("abc.foo.bar", "ar"));
assert( string_has_suffix("abc.foo.bar", "bar"));
assert(!string_has_suffix("abc.foo.bar", "xar"));
assert( string_has_suffix("abc.foo.bar", ".bar"));
assert( string_has_suffix("abc.foo.bar", "foo.bar"));
assert(!string_has_suffix("abc.foo.bar", "xoo.bar"));
assert(!string_has_suffix("abc.foo.bar", "foo.ba"));
assert( string_has_suffix("abc.foo.bar", ".foo.bar"));
assert( string_has_suffix("abc.foo.bar", "c.foo.bar"));
assert( string_has_suffix("abc.foo.bar", "abc.foo.bar"));
assert(!string_has_suffix("abc.foo.bar", "xabc.foo.bar"));
assert(!string_has_suffix("abc.foo.bar", "ac.foo.bar"));
assert( string_has_suffix("abc.foo.foo", ".foo"));
assert( string_has_suffix("abc.foo.foo", ".foo.foo"));
assert( string_has_suffix("abcdefgh", ""));
assert(!string_has_suffix("abcdefgh", " "));
assert( string_has_suffix("abcdefgh", "h"));
assert( string_has_suffix("abcdefgh", "gh"));
assert( string_has_suffix("abcdefgh", "fgh"));
assert(!string_has_suffix("abcdefgh", "agh"));
assert( string_has_suffix("abcdefgh", "abcdefgh"));
return 0;
}
// $ gcc -Wall string_has_suffix.c && ./a.out