Check if suffix and prefix of a string are palindromes | C ++ implementation of the approach ; Function to check whether the string is a palindrome ; reverse the string to compare with the original string ; check if both are same ; Function to check whether the string has prefix and suffix substrings of length greater than 1 which are palindromes . ; check all prefix substrings ; check if the prefix substring is a palindrome ; If we did not find any palindrome prefix of length greater than 1. ; check all suffix substrings , as the string is reversed now ; check if the suffix substring is a palindrome ; If we did not find a suffix ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPalindrome ( string r ) { string p = r ; reverse ( p . begin ( ) , p . end ( ) ) ; return ( r == p ) ; } bool CheckStr ( string s ) { int l = s . length ( ) ; int i ; for ( i = 2 ; i <= l ; i ++ ) { if ( isPalindrome ( s . substr ( 0 , i ) ) ) break ; } if ( i == ( l + 1 ) ) return false ; i = 2 ; for ( i = 2 ; i <= l ; i ++ ) { if ( isPalindrome ( s . substr ( l - i , i ) ) ) return true ; } return false ; } int main ( ) { string s = " abccbarfgdbd " ; if ( CheckStr ( s ) ) cout << " YES STRNEWLINE " ; else cout << " NO STRNEWLINE " ; return 0 ; }