📜  查找字符串是否以另一个给定字符串开头和字符串

📅  最后修改于: 2022-05-13 01:57:06.694000             🧑  作者: Mango

查找字符串是否以另一个给定字符串开头和字符串

给定一个字符串str 和一个角字符串cs,我们需要找出字符串str 是否以角字符串cs 开始和结束。
例子:

Input : str = "geeksmanishgeeks", cs = "geeks"
Output : Yes

Input : str = "shreya dhatwalia", cs = "abc"
Output : No

算法

  • 查找给定字符串str 以及角字符串cs 的长度。让这个长度分别为 n 和 cl。
  • 如果 cl>n,则返回 false,因为 cs 不能大于 str。
  • 否则,从 str 中找到长度为 cl 的前缀和后缀。如果前缀和后缀都与角字符串cs 匹配,则返回 true,否则返回 false。

C++
// CPP program to find if a given corner string
// is present at corners.
#include 
using namespace std;
 
bool isCornerPresent(string str, string corner)
{
    int n = str.length();
    int cl = corner.length();
 
    // If length of corner string is more, it
    // cannot be present at corners.
    if (n < cl)
       return false;
 
    // Return true if corner string is present at
    // both corners of given string.
    return (str.substr(0, cl).compare(corner) == 0 &&
            str.substr(n-cl, cl).compare(corner) == 0);
}
 
// Driver code
int main()
{
   string str = "geeksforgeeks";
   string corner = "geeks";
   if (isCornerPresent(str, corner))
      cout << "Yes";
   else
      cout << "No";
   return 0;
}


Java
// Java program to find if a given corner
// string is present at corners.
import java.io.*;
class GFG {
     
    static boolean isCornerPresent(String str,
                                   String corner)
    {
        int n = str.length();
        int cl = corner.length();
 
        // If length of corner string
        // is more, it cannot be present
        // at corners.
        if (n < cl)
        return false;
 
        // Return true if corner string
        // is present at both corners
        // of given string.
        return (str.substring(0, cl).equals(corner) &&
                str.substring(n - cl, n).equals(corner));
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        String str = "geeksforgeeks";
        String corner = "geeks";
        if (isCornerPresent(str, corner))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
 
// This code is contributed by Manish_100


Python3
# Python program to find
# if a given corner string
# is present at corners.
 
def isCornerPresent(str, corner) :
 
    n = len(str)
    cl = len(corner)
 
    # If length of corner
    # string is more, it
    # cannot be present
    # at corners.
    if (n < cl) :
        return False
 
    # Return true if corner
    # string is present at
    # both corners of given
    # string.
    return ((str[: cl] == corner) and
            (str[n - cl :] == corner))
 
# Driver Code
str = "geeksforgeeks"
corner = "geeks"
if (isCornerPresent(str, corner)) :
    print ("Yes")
else :
    print ("No")
 
# This code is contributed by
# Manish Shaw(manishshaw1)


C#
// C# program to find if a
// given corner string is
// present at corners.
using System;
 
class GFG
{
static bool isCornerPresent(string str,
                            string corner)
{
    int n = str.Length;
    int cl = corner.Length;
 
    // If length of corner
    // string is more, it
    // cannot be present
    // at corners.
    if (n < cl)
        return false;
 
    // Return true if corner
    // string is present at
    // both corners of given
    // string.
    return (str.Substring(0,
            cl).Equals(corner) &&
            str.Substring(n - cl,
            cl).Equals(corner));
}
 
// Driver Code
static void Main ()
{
    string str = "geeksforgeeks";
    string corner = "geeks";
    if (isCornerPresent(str, corner))
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
}
 
// This code is contributed by
// Manish Shaw(manishshaw1)


PHP


Javascript


输出 :

Yes