Find if a String ends With the Given Substring in C++ Last Updated : 01 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report You are given two strings. The task is to check if the main string ends with another string in C++. Example Input: mainString = "Hello! Geek"; targetString = "Geek" Output: Hello! Geek ends with Geek.We can find if the given string ends with the target string using the following method: Checking if the Main String ends with the Given SubstringWe can use the std::string::compare() method of string to compare the substring at the end of the main string with the given target string. If they match, the string::compare() function will return 0, otherwise, it will return the non-zero values. C++ Program to Check if String Ends with Another StringThe below example demonstrates the use of the compare function to find if a string ends with another string or not. C++ // C++ Program to Check if String Ends with Another String #include <iostream> #include <string> using namespace std; // Function to check if a string ends with another string bool endsWith(const string& fullString, const string& ending) { // Check if the ending string is longer than the full // string if (ending.size() > fullString.size()) return false; // Compare the ending of the full string with the target // ending return fullString.compare(fullString.size() - ending.size(), ending.size(), ending) == 0; } int main() { // Sample strings for demonstration string str = "Hello! Geek"; string targetStr = "Geek"; // Check if the string ends with the target string if (endsWith(str, targetStr)) { cout << str << " ends with " << targetStr << endl; } else { cout << str << " does not end with " << targetStr << endl; } return 0; } OutputHello! Geek ends with Geek From C++20 or later we can also use the ends_with function to check if a string ends with another string. Comment More infoAdvertise with us Next Article How to Find the Length of a Substring in a char Array? V vw830kppu Follow Improve Article Tags : C++ Programs C++ cpp-string CPP Examples Practice Tags : CPP Similar Reads How to Split a C++ String into a Vector of Substrings? In C++, splitting a string into a vector of substrings means we have to split the given string into a substring based on a given delimiter and store each substring in a vector. In this article, we will learn how to split a string into a vector of substrings in C++. Example: Input: str= "Hello, I am 2 min read std::string::find_last_of in C++ with Examples The std::string::find_last_of is a string class member function which is used to find the index of last occurrence of any characters in a string. If the character is present in the string then it returns the index of the last occurrence of that character in the string else it returns string::npos.He 3 min read Longest substring that starts with X and ends with Y Given a string str, two characters X and Y. The task is to find the length of the longest substring that starts with X and ends with Y. It is given that there always exists a substring that starts with X and ends with Y. Examples: Input: str = "QWERTYASDFZXCV", X = 'A', Y = 'Z' Output: 5 Explanation 10 min read How to Find and Replace All Occurrences of a Substring in a C++ String? In C++, strings are sequences of characters that are used to represent textual data. In this article, we will learn how to find and replace all the occurrences of a particular substring in the given string. For Example, Input: str = "Lokesh is a good programmer, but Lokesh is still in the learning p 2 min read How to Find the Length of a Substring in a char Array? In C++, a substring is a string inside another string that is the contiguous part of the string. In this article, we will learn how to find the length of a substring in a char array in C++. Example Input: str[] = "Hello, World!";substr= "World";Output: Length of substring World is: 5Finding Length o 2 min read std::string::rfind in C++ with Examples The std::string::rfind is a string class member function that is used to search the last occurrence of any character in the string. If the character is present in the string then it returns the index of the last occurrence of that character in the string else it will return string::npos which denote 5 min read Like