Find if a string starts and ends with another given string Last Updated : 20 Feb, 2023 Comments Improve Suggest changes Like Article Like Report 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 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. Implementation: C++ // CPP program to find if a given corner string // is present at corners. #include <bits/stdc++.h> 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 <?php // PHP program to find if a // given corner string is // present at corners. function isCornerPresent($str, $corner) { $n = strlen($str); $cl = strlen($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 (!strcmp(substr($str, 0, $cl), $corner) && !strcmp(substr($str, $n - $cl, $cl), $corner)); } // Driver Code $str = "geeksforgeeks"; $corner = "geeks"; if (isCornerPresent($str, $corner)) echo ("Yes"); else echo ("No"); // This code is contributed by // Manish Shaw(manishshaw1) ?> JavaScript <script> // JavaScript program to find if a given corner string // is present at corners. function isCornerPresent(str, corner) { var n = str.length; var 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).localeCompare(corner) === 0 && str.substring(n - cl, n).localeCompare(corner) === 0 ); } // Driver code var str = "geeksforgeeks"; var corner = "geeks"; if (isCornerPresent(str, corner)) document.write("Yes"); else document.write("No"); </script> OutputYes Time Complexity: O(n)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Find if a string starts and ends with another given string M Manish_100 Follow Improve Article Tags : Strings DSA Basic Coding Problems Practice Tags : Strings Similar Reads Different substrings in a string that start and end with given strings Given a string s and two other strings begin and end, find the number of different substrings in the string which begin and end with the given begin and end strings. Examples: Input : s = "geeksforgeeks" begin = "geeks" end = "for" Output : 1 Input : s = "vishakha" begin = "h" end = "a" Output : 2 T 9 min read Find a String in given Array of Strings using Binary Search Given a sorted array of Strings arr and a string x, The task is to find the index of x in the array using the Binary Search algorithm. If x is not present, return -1.Examples:Input: arr[] = {"contribute", "geeks", "ide", "practice"}, x = "ide"Output: 2Explanation: The String x is present at index 2. 6 min read Minimize partitions in given string to get another string Given two strings A and B, print the minimum number of slices required in A to get another string B. In case, if it is not possible to get B from A, then print "-1". Examples : Input: A = "geeksforgeeks", B = "ksgek"Output: 5Explanation: g | ee | ks | forge | ek | s : minimum 5 slices are required t 15+ min read Searching For Characters and Substring in a String in Java Efficient String manipulation is very important in Java programming especially when working with text-based data. In this article, we will explore essential methods like indexOf(), contains(), and startsWith() to search characters and substrings within strings in Java.Searching for a Character in a 5 min read Check if a string is substring of another Given two strings txt and pat, the task is to find if pat is a substring of txt. If yes, return the index of the first occurrence, else return -1.Examples : Input: txt = "geeksforgeeks", pat = "eks"Output: 2Explanation: String "eks" is present at index 2 and 9, so 2 is the smallest index.Input: txt 8 min read Like