给定两个字符串s1和s2,请检查s1是否为s2的后缀。或者简单地说,我们需要确定字符串s2是否以字符串s1结尾。
例子 :
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
方法1(编写我们自己的代码)
C++
// CPP program to find if a string is
// suffix of another
#include
#include
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
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
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
PHP
$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
?>
C++
// CPP program to find if a string is
// suffix of another
#include
#include
#include
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
Python3
# 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#
// 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
方法2(在C++中使用boost库)
由于std :: 字符串类不提供任何endWith()函数,该函数以一个字符串结尾另一个字符串,因此我们将使用Boost库。确保包含#include boost / algorithm / 字符串.hpp和#include 字符串以正常运行代码。
C++
// CPP program to find if a string is
// suffix of another
#include
#include
#include
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
Python3
# 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#
// 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