How to check if a string ends with a specified substring in C++

1 Answer

0 votes
#include <iostream>
#include <algorithm>
#include <string>

bool endsWith(const std::string& str, const std::string& ending) {
    if (ending.size() > str.size()) {
        return false;
    }
    
    return std::equal(ending.rbegin(), ending.rend(), str.rbegin());
}

int main() {
    std::string str = "c c++ java python";
    std::string tofind = "python";

    std::cout << endsWith(str, tofind) << std::endl;
}



/*
run:

1

*/

 



answered May 25, 2024 by avibootz
edited May 25, 2024 by avibootz

Related questions

1 answer 97 views
2 answers 126 views
1 answer 129 views
1 answer 167 views
1 answer 166 views
1 answer 250 views
...