![]() |
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++
Output
Hello! Geek ends with Geek From C++20 or later we can also use the |
Reffered: https://www.geeksforgeeks.org
C++ |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |