Skip to content

Instantly share code, notes, and snippets.

@royratcliffe
Last active January 30, 2024 08:27
Show Gist options
  • Save royratcliffe/670884e9d763f6ead09a50fb5121cd09 to your computer and use it in GitHub Desktop.
Save royratcliffe/670884e9d763f6ead09a50fb5121cd09 to your computer and use it in GitHub Desktop.
C++
#include <cassert>
/*!
* \brief True if the first string ends with the second string.
* \param ends String with some required ending for which to test.
* \param with Ending to test the first string against.
*/
bool ends_with(const std::string &ends, const std::string &with) {
return ends.length() >= with.length() &&
ends.compare(ends.length() - with.length(), with.length(), with) == 0;
}
int ends_with_test(int argc, char **argv) {
assert(ends_with("abc", "c"));
assert(ends_with("abc", ""));
assert(!ends_with("abc", "z"));
assert(ends_with("a.bmp", ".bmp"));
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment