String ends with?

Complete the solution so that it returns true if the first argument(string) passed in ends with the 2nd argument (also a string).

Examples:

solution('abc', 'bc') // returns true
solution('abc', 'd') // returns false
solution('abc', 'bc') # returns true
solution('abc', 'd') # returns false
solution('abc', 'bc') # returns true
solution('abc', 'd') # returns false
solution("abc", "bc") // returns true
solution("abc", "d") // returns false
solution("abc", "bc"). % match
\+ solution("abc", "d"). % no match

Solutions

🐍 Python

def solution(str, e):
    return str[-len(e):] == e

👴 C

#include <string.h>

char solution(const char *str, const char *end)
{
    int bias = strlen(str) - strlen(end);
    return bias < 0 ? 0 : !strcmp( (str+bias), end );
}

Last updated