# Find if a string starts and ends with another given string

Given a string str and a corner string cs, we need to find out whether the string str starts and ends with the corner string cs or not.

Examples:

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

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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Algorithm

• Find length of given string str as well as corner string cs. Let this length be n and cl respectively.
• If cl>n, return false as cs can’t be greater than str.
• Otherwise, find the prefix and suffix of length cl from str. If both prefix and suffix match with corner string cs, return true otherwise return 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

 ` `

Output :

```Yes
```

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Improved By : Manish_100, manishshaw1

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.