C++ : How to check if a String Ends With an Another given String

The C++ string class internally stores characters in a char array, but all memory management, allocation, and null termination is handled by the string class itself, which is why it is simple to use. The length of a C++ string can be changed at runtime due to dynamic memory allocation, which is similar to vectors. Because the string class is a container class, we can iterate over all of its characters with an iterator, just like we can with vectors, sets, and maps, but we usually use a simple for loop to iterate over the characters and index them with the [] operator.

Examples:

1)Case sensitive

Input:

string mainstring = "This is BTechGeeks"

 string matchstring = "btechgeeks"

Output:

Do not ends with given string

2)Case insensitive

Input:

string mainstring = "This is BTechGeeks"

 string matchstring = "btechGeeks"

Output:

ends with given string

Check to see if a string ends with another string.

There are several methods to check string ends with an another given string some of them are:

Method #1 :Using std::compare

The string class has a public member function called compare(). It compares the string object’s (or a substring’s) value to the character sequence defined by its arguments.
For each string, compare() will process several arguments, allowing you to select a substring by index and length.

To see if a main string ends with a given string, we can only look at the main string’s last n characters, where n is the length of the given string. To find the last occurrence of a given string from location (Size of Main string – size of given string) use std:string::compare().

Below is the implementation:

1)Case sensitive

#include <bits/stdc++.h>
using namespace std;
int main()
{
    // given main string
    string mainstring = "This is BTechGeeks";
    // given to match string
    string matchstring = "btechgeeks";
    // using compare function to cheeck if the given string
    // ends with match string
    if (mainstring.size() >= matchstring.size()
        && mainstring.compare(
               mainstring.size() - matchstring.size(),
               matchstring.size(), matchstring)
               == 0)
        cout << "ends with given string" << endl;
    else
        cout << "Do not ends with given string" << endl;
}

Output:

ends with given string

Method #2:using std::all_of()

The C++ function is defined in the STL library <algorithm>. This feature works on the entire range of array elements, saving time over running a loop to review each element one at a time. It checks each element for a given property and returns true if each element in the range satisfies the specified property otherwise, it returns false.

Let us see the implementation:

1)case sensitive

#include <bits/stdc++.h>
using namespace std;
int main()
{
    // given main string
    string mainstring = "This is BTechGeeks";
    // given to match string
    string matchstring = "btechgeeks";
    // using all_of function to cheeck if the given string
    // ends with match string
    auto it = matchstring.begin();
    if (mainstring.size() >= matchstring.size()
        && all_of(
            next(mainstring.begin(),
                 mainstring.size() - matchstring.size()),
            mainstring.end(),
            [&it](const char& c) { return c == *(it++); }))
        cout << "ends with given string" << endl;
    else
        cout << "Do not ends with given string" << endl;
}

Output:

Do not ends with given string

2)Case insensitive

#include <bits/stdc++.h>
using namespace std;
int main()
{
    // given main string
    string mainstring = "This is BTechGeeks";
    // given to match string
    string matchstring = "btechgeeks";
    // using all_of function to cheeck if the given string
    // ends with match string
    auto it = matchstring.begin();
    if (mainstring.size() >= matchstring.size()
        && all_of(
            next(mainstring.begin(),
                 mainstring.size() - matchstring.size()),
            mainstring.end(), [&it](const char& c) {
                return ::tolower(c) == ::tolower(*(it++));
            }))
        cout << "ends with given string" << endl;
    else
        cout << "Do not ends with given string" << endl;
}

Output:

ends with given string

Method #3:Using ends_with in boost library

The algorithm library in Boost supports both case sensitive and case insensitive implementations of the endsWith() function for strings.

Header file :<boost/algorithm/string.hpp>

1)Case sensitive

Syntax:

boost::algorithm::ends_with(mainstring, matchString) ;

Below is the implementation:

#include <bits/stdc++.h>
using namespace std;
#include <boost/algorithm/string.hpp>
int main()
{
    // given main string
    string mainstring = "This is BTechGeeks";
    // given to match string
    string matchstring = "btechgeeks";
    // using boost function(case sensitive)
    if (boost::algorithm::ends_with(mainstring,
                                    matchstring))
        cout << "ends with given string" << endl;
    else
        cout << "Do not ends with given string" << endl;
}

Output:

Do not ends with given string

2)Case insensitive

Syntax:

boost::algorithm::iends_with(mainstring, matchString) ;

Below is the implementation:

#include <bits/stdc++.h>
using namespace std;
#include <boost/algorithm/string.hpp>
int main()
{
    // given main string
    string mainstring = "This is BTechGeeks";
    // given to match string
    string matchstring = "btechgeeks";
    // using boost function(case insensitive)
    if (boost::algorithm::iends_with(mainstring,
                                     matchstring))
        cout << "ends with given string" << endl;
    else
        cout << "Do not ends with given string" << endl;
}

Output:

ends with given string

 
Related Programs: