Find if a String ends With the Given Substring in C++ Last Updated : 01 Feb, 2024 Comments Improve Suggest changes 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 Find if a String ends With the Given Substring in C++ 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 Smallest string which not a subsequence of the given string Given a string str, consisting of lowercase alphabets, the task is to find the shortest string which is not a subsequence of the given string. If multiple strings exist, then print any one of them. Examples: Input: str = "abaabcc" Output: d Explanation: One of the possible shortest string which is n 6 min read C++ Program To Find Length Of The Longest Substring Without Repeating Characters Given a string str, find the length of the longest substring without repeating characters. For âABDEFGABEFâ, the longest substring are âBDEFGAâ and "DEFGAB", with length 6.For âBBBBâ the longest substring is âBâ, with length 1.For "GEEKSFORGEEKS", there are two longest substrings shown in the below 6 min read Frequency of a substring in a string | Set 2 Given a string str of length N and a substring pattern of length M, the task is to find the frequency of occurrences of pattern as a substring in the given string. If pattern is present in the string str, then print "Yes" with the count of its occurrence. Otherwise, print "No". Examples: Input: str 6 min read C++ Program To Check If A String Is Substring Of Another Given two strings s1 and s2, find if s1 is a substring of s2. If yes, return the index of the first occurrence, else return -1. Examples :Â Input: s1 = "for", s2 = "geeksforgeeks" Output: 5 Explanation: String "for" is present as a substring of s2. Input: s1 = "practice", s2 = "geeksforgeeks" Output 4 min read Like