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
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++
#include <bits/stdc++.h>
using namespace std;
bool isCornerPresent(string str, string corner)
{
int n = str.length();
int cl = corner.length();
if (n < cl)
return false ;
return (str.substr(0, cl).compare(corner) == 0 &&
str.substr(n-cl, cl).compare(corner) == 0);
}
int main()
{
string str = "geeksforgeeks" ;
string corner = "geeks" ;
if (isCornerPresent(str, corner))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.io.*;
class GFG {
static boolean isCornerPresent(String str,
String corner)
{
int n = str.length();
int cl = corner.length();
if (n < cl)
return false ;
return (str.substring( 0 , cl).equals(corner) &&
str.substring(n - cl, n).equals(corner));
}
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" );
}
}
|
Python3
def isCornerPresent( str , corner) :
n = len ( str )
cl = len (corner)
if (n < cl) :
return False
return (( str [: cl] = = corner) and
( str [n - cl :] = = corner))
str = "geeksforgeeks"
corner = "geeks"
if (isCornerPresent( str , corner)) :
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static bool isCornerPresent( string str,
string corner)
{
int n = str.Length;
int cl = corner.Length;
if (n < cl)
return false ;
return (str.Substring(0,
cl).Equals(corner) &&
str.Substring(n - cl,
cl).Equals(corner));
}
static void Main ()
{
string str = "geeksforgeeks" ;
string corner = "geeks" ;
if (isCornerPresent(str, corner))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
function isCornerPresent( $str ,
$corner )
{
$n = strlen ( $str );
$cl = strlen ( $corner );
if ( $n < $cl )
return false;
return (! strcmp ( substr ( $str , 0,
$cl ), $corner ) &&
! strcmp ( substr ( $str , $n -
$cl , $cl ), $corner ));
}
$str = "geeksforgeeks" ;
$corner = "geeks" ;
if (isCornerPresent( $str , $corner ))
echo ( "Yes" );
else
echo ( "No" );
?>
|
Javascript
<script>
function isCornerPresent(str, corner) {
var n = str.length;
var cl = corner.length;
if (n < cl) return false ;
return (
str.substring(0, cl).localeCompare(corner) === 0 &&
str.substring(n - cl, n).localeCompare(corner) === 0
);
}
var str = "geeksforgeeks" ;
var corner = "geeks" ;
if (isCornerPresent(str, corner)) document.write( "Yes" );
else document.write( "No" );
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Last Updated :
20 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...