Check if a string is suffix of another
Given two strings s1 and s2, check if s1 is a suffix of s2. Or in simple words we need to find whether string s2 ends with string s1.
Examples :
Input : s1 = "geeks" and s2 = "geeksforgeeks" Output : Yes Input : s1 = "world", s2 = "my first code is hello world" Output : Yes Input : s1 = "geeks" and s2 = "geeksforGeek" Output : No
Method 1 (Writing our own code)
C++
// CPP program to find if a string is // suffix of another #include <iostream> #include <string> using namespace std; bool isSuffix(string s1, string s2) { int n1 = s1.length(), n2 = s2.length(); if (n1 > n2) return false; for (int i=0; i<n1; i++) if (s1[n1 - i - 1] != s2[n2 - i - 1]) return false; return true; } int main() { string s1 = "geeks", s2 = "geeksforgeeks"; // Test case-sensitive implementation // of endsWith function bool result = isSuffix(s1, s2); if (result) cout << "Yes"; else cout << "No"; return 0; } |
Java
// Java program to find if a string is // suffix of another class GFG { static boolean isSuffix(String s1, String s2) { int n1 = s1.length(), n2 = s2.length(); if (n1 > n2) return false; for (int i=0; i<n1; i++) if (s1.charAt(n1 - i - 1) != s2.charAt(n2 - i - 1)) return false; return true; } public static void main(String []args) { String s1 = "geeks", s2 = "geeksforgeeks"; // Test case-sensitive implementation // of endsWith function boolean result = isSuffix(s1, s2); if (result) System.out.println( "Yes"); else System.out.println("No"); } } // This code is contributed by iAyushRaj |
Python 3
# Python 3 program to find if a # string is suffix of another def isSuffix(s1, s2): n1 = len(s1) n2 = len(s2) if (n1 > n2): return False for i in range(n1): if(s1[n1 - i - 1] != s2[n2 - i - 1]): return False return True # Driver Code if __name__ == "__main__": s1 = "geeks" s2 = "geeksforgeeks" # Test case-sensitive implementation # of endsWith function result = isSuffix(s1, s2) if (result): print("Yes") else: print( "No") # This code is contributed # by ChitraNayal |
C#
// C# program to find if a string is // suffix of another using System; class GFG { static bool isSuffix(string s1, string s2) { int n1 = s1.Length, n2 = s2.Length; if (n1 > n2) return false; for (int i=0; i<n1; i++) if (s1[n1 - i - 1] != s2[n2 - i - 1]) return false; return true; } public static void Main() { string s1 = "geeks", s2 = "geeksforgeeks"; // Test case-sensitive implementation // of endsWith function bool result = isSuffix(s1, s2); if (result) Console.WriteLine( "Yes"); else Console.WriteLine("No"); } } // This code is contributed by iAyushRaj |
PHP
<?php // PHP program to find if a // string is suffix of another function isSuffix($s1, $s2) { $n1 = ($s1); $n2 = strlen($s2); if ($n1 > $n2) return false; for ($i = 0; $i < $n1; $i++) if ($s1[$n1 - $i - 1] != $s2[$n2 - $i - 1]) return false; return true; } // Driver Code $s1 = "geeks"; $s2 = "geeksforgeeks"; // Test case-sensitive implementation // of endsWith function $result = isSuffix($s1, $s2); if ($result) echo "Yes"; else echo "No"; // This code is contributed by m_kit ?> |
Yes
Method 2 (Using boost library in C++)
Since std::string class does not provide any endWith() function which a string ends with another string so we will be using Boost Library. Make sure to include #include boost/algorithm/string.hpp and #include string to run the code fine.
C++
// CPP program to find if a string is // suffix of another #include <boost/algorithm/string.hpp> #include <iostream> #include <string> using namespace std; int main() { string s1 = "geeks", s2 = "geeksforgeeks"; // Test case-sensitive implementation // of endsWith function bool result = boost::algorithm::ends_with(s2, s1); if (result) cout << "Yes"; else cout << "No"; return 0; } |
Java
// Java program to find if a string is // suffix of another class GFG { public static void main(String[] args) { String s1 = "geeks", s2 = "geeksforgeeks"; // Test case-sensitive implementation // of endsWith function boolean result = s2.endsWith(s1); if (result) System.out.println("Yes"); else System.out.println("No"); } } // This code is contributed by 29AjayKumar |
C#
// C# program to find if a string is // suffix of another using System; class GFG { // Driver code public static void Main(String[] args) { String s1 = "geeks", s2 = "geeksforgeeks"; // Test case-sensitive implementation // of endsWith function bool result = s2.EndsWith(s1); if (result) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } // This code contributed by Rajput-Ji |
Yes
Recommended Posts:
- Check if suffix and prefix of a string are palindromes
- Find the longest sub-string which is prefix, suffix and also present inside the string
- Find the longest sub-string which is prefix, suffix and also present inside the string | Set 2
- Print the longest prefix of the given string which is also the suffix of the same string
- String from prefix and suffix of given two strings
- Count of distinct substrings of a string using Suffix Array
- Count of distinct substrings of a string using Suffix Trie
- String slicing in Python to check if a string can become empty by recursive deletion
- Check if a string can be converted to another string by replacing vowels and consonants
- Queries to check if string B exists as substring in string A
- Check if a string can be obtained by rotating another string d places
- Check if a string can become empty by recursively deleting a given sub-string
- Check if a string can be obtained by rotating another string 2 places
- Check whether second string can be formed from characters of first string
- Check if a string contains a palindromic sub-string of even length
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



