If you were to generalize this problem, to say "Does a string str
end with a string suffix
?" you could do the following:
#include <string.h>
int endswith(char* str, char* suffix)
{
// assuming that str and suffix are not NULL
return strcmp(str + strlen(str) - strlen(suffix), suffix) == 0;
}
It would be trivial to add a check for NULL to the function as well:
#include <string.h>
int endswith(char* str, char* suffix)
{
return str && suffix && strcmp(str + strlen(str) - strlen(suffix), suffix) == 0;
}