Check if a string is suffix of another - GeeksforGeeks

03 May, 2021
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++
Java
Python3
C#
PHP
Javascript
// 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 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 program to find if a# string is suffix of anotherdef 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 Codeif __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# 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 program to find if a// string is suffix of anotherfunction 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?>
<script> // Javascript program to find if// a string is suffix of anotherfunction isSuffix(s1, s2){    let n1 = s1.length, n2 = s2.length;    if (n1 > n2)        return false;             for(let i = 0; i < n1; i++)        if (s1[n1 - i - 1] != s2[n2 - i - 1])            return false;             return true;} // Driver codelet s1 = "geeks", s2 = "geeksforgeeks";   // Test case-sensitive implementation// of endsWith functionlet result = isSuffix(s1, s2); if (result)    document.write( "Yes");else    document.write("No");     // This code is contributed by decode2207 </script>
Yes
 
Method 2 (Using boost library in C++) Since std::string class does not provide any endWith() function in 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++
Java
Python3
C#
Javascript
// 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 program to find if a string is// suffix of anotherclass 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
# Python3 program to find if a string is# suffix of another if __name__ == '__main__':    s1 = "geeks";    s2 = "geeksforgeeks";         # Test case-sensitive implementation    # of endsWith function    result = s2.endswith(s1);         if (result):        print("Yes");    else:        print("No");     # This code is contributed by Rajput-Ji
// C# program to find if a string is// suffix of anotherusing 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
<script>// Javascript program to find if a string is// suffix of another         let s1 = "geeks", s2 = "geeksforgeeks";    // Test case-sensitive implementation    // of endsWith function    let result = s2.endsWith(s1);    if (result)            document.write("Yes");    else        document.write("No");                  // This code is contributed by avanitrachhadiya2155</script>
Yes
 
YouTubeGeeksforGeeks500K subscribersCheck if a string is suffix of another | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 3:03•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=rkWYCL9joY4" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
jit_t
ukasp
iAyushRaj
29AjayKumar
Rajput-Ji
decode2207
avanitrachhadiya2155
Searching
Strings
Searching
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Best First Search (Informed Search)
Split the given array into K sub-arrays such that maximum sum of all sub arrays is minimum
3 Different ways to print Fibonacci series in Java
Find whether an array is subset of another array | Added Method 5
Recursive Programs to find Minimum and Maximum elements of array
Reverse a string in Java
Write a program to reverse an array or string
Longest Common Subsequence | DP-4
Write a program to print all permutations of a given string
C++ Data Types