if(s1.charAt(n1 - i - 1) != s2.charAt(n2 - i - 1))
returnfalse;
returntrue;
}
publicstaticvoidmain(String []args)
{
String s1 = "geeks", s2 = "geeksforgeeks";
// Test case-sensitive implementation
// of endsWith function
booleanresult = isSuffix(s1, s2);
if(result)
System.out.println( "Yes");
else
System.out.println("No");
}
}
// This code is contributed by iAyushRaj
chevron_right
filter_none
Python 3
filter_none
edit close
play_arrow
link brightness_4 code
# Python 3 program to find if a
# string is suffix of another
defisSuffix(s1, s2):
n1 =len(s1)
n2 =len(s2)
if(n1 > n2):
returnFalse
fori inrange(n1):
if(s1[n1 -i -1] !=s2[n2 -i -1]):
returnFalse
returnTrue
# 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
chevron_right
filter_none
C#
filter_none
edit close
play_arrow
link brightness_4 code
// C# program to find if a string is
// suffix of another
usingSystem;
classGFG
{
staticboolisSuffix(strings1, strings2)
{
intn1 = s1.Length, n2 = s2.Length;
if(n1 > n2)
returnfalse;
for(inti=0; i<n1; i++)
if(s1[n1 - i - 1] != s2[n2 - i - 1])
returnfalse;
returntrue;
}
publicstaticvoidMain()
{
strings1 = "geeks", s2 = "geeksforgeeks";
// Test case-sensitive implementation
// of endsWith function
boolresult = isSuffix(s1, s2);
if(result)
Console.WriteLine( "Yes");
else
Console.WriteLine("No");
}
}
// This code is contributed by iAyushRaj
chevron_right
filter_none
PHP
filter_none
edit close
play_arrow
link brightness_4 code
<?php
// PHP program to find if a
// string is suffix of another
functionisSuffix($s1, $s2)
{
$n1= ($s1);
$n2= strlen($s2);
if($n1> $n2)
returnfalse;
for($i= 0; $i< $n1; $i++)
if($s1[$n1- $i- 1] != $s2[$n2- $i- 1])
returnfalse;
returntrue;
}
// 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
?>
chevron_right
filter_none
Output:
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++
filter_none
edit close
play_arrow
link brightness_4 code
// CPP program to find if a string is
// suffix of another
#include <boost/algorithm/string.hpp>
#include <iostream>
#include <string>
usingnamespacestd;
intmain()
{
string s1 = "geeks", s2 = "geeksforgeeks";
// Test case-sensitive implementation
// of endsWith function
boolresult = boost::algorithm::ends_with(s2, s1);
if(result)
cout << "Yes";
else
cout << "No";
return0;
}
chevron_right
filter_none
Java
filter_none
edit close
play_arrow
link brightness_4 code
// Java program to find if a string is
// suffix of another
classGFG
{
publicstaticvoidmain(String[] args)
{
String s1 = "geeks", s2 = "geeksforgeeks";
// Test case-sensitive implementation
// of endsWith function
booleanresult = s2.endsWith(s1);
if(result)
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by 29AjayKumar
chevron_right
filter_none
Python3
filter_none
edit close
play_arrow
link brightness_4 code
# 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
chevron_right
filter_none
C#
filter_none
edit close
play_arrow
link brightness_4 code
// C# program to find if a string is
// suffix of another
usingSystem;
classGFG
{
// Driver code
publicstaticvoidMain(String[] args)
{
String s1 = "geeks", s2 = "geeksforgeeks";
// Test case-sensitive implementation
// of endsWith function
boolresult = s2.EndsWith(s1);
if(result)
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code contributed by Rajput-Ji
chevron_right
filter_none
Output :
Yes
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy