In this article we will discuss both case sensitive and insensitive implementations to check if a string ends with an another given string.

In previous article we discussed different implementations of startsWith() function for std::string. Here on similar lines we will discuss different implementations of endsWith() function.

In c++, std::string class does not provides any endsWith() function to check if a string ends with an another given string. Let’s see how to do that using std::string::compare() and Boost Library.

std::string endWith() using std::string::compare() and std::all_of()

std::string class provides a member function compare() with different overloaded versions. We will use one of its overloaded version i.e.

int compare (size_t pos, size_t len, const string& str) const;

It accepts a string as argument to match, starting position for match and number of characters to match. If string matches then it returns 0 else returns > 0 or < 0 based on difference in ascii values of first unmatched character.

To check if a main string ends with a given string, we should look into last n characters only for main string, where n is the size of given string. Let’s use std:string::compare() to find the last occurrence of given string from position (Size of Main string – size of given string).

Case sensitive Implementation of endsWith using std::string::compare()

/*
 * Case Sensitive Implementation of endsWith()
 * It checks if the string 'mainStr' ends with given string 'toMatch'
 */
bool endsWith(const std::string &mainStr, const std::string &toMatch)
{
	if(mainStr.size() >= toMatch.size() &&
			mainStr.compare(mainStr.size() - toMatch.size(), toMatch.size(), toMatch) == 0)
			return true;
		else
			return false;
}

Case sensitive Implementation of endsWith using std::all_of()

/*
 * Case Sensitive Implementation of endsWith()
 * It checks if the string 'mainStr' ends with given string 'toMatch'
 */
bool endsWith_secApproach(const std::string &mainStr, const std::string &toMatch)
{
	auto it = toMatch.begin();
		return mainStr.size() >= toMatch.size() &&
				std::all_of(std::next(mainStr.begin(),mainStr.size() - toMatch.size()), mainStr.end(), [&it](const char & c){
					return c == *(it++)  ;
		} );
}

 

Case Insensitive Implementation of endsWith using std::all_of()

/*
 * Case Insensitive Implementation of endsWith()
 * It checks if the string 'mainStr' ends with given string 'toMatch'
 */
bool endsWithCaseInsensitive(std::string mainStr, std::string toMatch)
{
	auto it = toMatch.begin();
	return mainStr.size() >= toMatch.size() &&
			std::all_of(std::next(mainStr.begin(),mainStr.size() - toMatch.size()), mainStr.end(), [&it](const char & c){
				return ::tolower(c) == ::tolower(*(it++))  ;
	} );


}

Check complete example as follows,
#include <iostream>
#include <string>
#include <algorithm>

/*
 * Case Sensitive Implementation of endsWith()
 * It checks if the string 'mainStr' ends with given string 'toMatch'
 */
bool endsWith(const std::string &mainStr, const std::string &toMatch)
{
	if(mainStr.size() >= toMatch.size() &&
			mainStr.compare(mainStr.size() - toMatch.size(), toMatch.size(), toMatch) == 0)
			return true;
		else
			return false;
}

/*
 * Case Sensitive Implementation of endsWith()
 * It checks if the string 'mainStr' ends with given string 'toMatch'
 */
bool endsWith_secApproach(const std::string &mainStr, const std::string &toMatch)
{
	auto it = toMatch.begin();
		return mainStr.size() >= toMatch.size() &&
				std::all_of(std::next(mainStr.begin(),mainStr.size() - toMatch.size()), mainStr.end(), [&it](const char & c){
					return c == *(it++)  ;
		} );
}

/*
 * Case Insensitive Implementation of endsWith()
 * It checks if the string 'mainStr' ends with given string 'toMatch'
 */
bool endsWithCaseInsensitive(std::string mainStr, std::string toMatch)
{
	auto it = toMatch.begin();
	return mainStr.size() >= toMatch.size() &&
			std::all_of(std::next(mainStr.begin(),mainStr.size() - toMatch.size()), mainStr.end(), [&it](const char & c){
				return ::tolower(c) == ::tolower(*(it++))  ;
	} );


}

int main()
{
	std::string mainStr = "This is the sample STRing";
	std::string toMatch = "STRing";

	// Test case-sensitive implementation of endsWith() function
	bool result = endsWith(mainStr , toMatch);
	std::cout<<result<<std::endl;

	// Test case-sensitive implementation of endsWith_secApproach() function
	result = endsWith_secApproach(mainStr , toMatch);
	std::cout<<result<<std::endl;

	// Test case-insensitive implementation of endsWith function
	result = endsWithCaseInsensitive(toMatch, "String");

	std::cout<<result<<std::endl;
}

Output:
1
1
1

std::string endsWith() using Boost Library

Boost’s algorithm library provides implementation of both case sensitive and insensitive implementation of endsWith() function for string i.e.

Case Sensitive Version :

boost::algorithm::ends_with(mainString, toMatchString) ;

Case Insensitive Version :
boost::algorithm::iends_with(mainString, toMatchString) ;


Header File required : <boost/algorithm/string.hpp>

Checkout complete example as follows,

#include <iostream>
#include <string>
#include <boost/algorithm/string.hpp>


int main()
{

	std::string mainStr = "This is the sample STRing";
	std::string toMatch = "STRing";

	// Test case-sensitive implementation of endsWith function
	bool result = boost::algorithm::ends_with(mainStr , toMatch);

	std::cout<<result<<std::endl;

	// Test case-insensitive implementation of endsWith function
	result = boost::algorithm::iends_with(mainStr, "string");

	std::cout<<result<<std::endl;
}

Output:
1
1

 

Subscribe with us to join a list of 2000+ Programmers for weekly newsletter.