Check if suffix and prefix of a string are palindromes | 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
< ? php function isPalindrome ( $ r ) { $ p = $ r ; strrev ( $ p ) ; return ( $ r == $ p ) ; } function CheckStr ( $ s ) { $ l = strlen ( $ s ) ; for ( $ i = 2 ; $ i <= $ l ; $ i ++ ) { if ( isPalindrome ( substr ( $ s , 0 , $ i ) ) ) break ; } if ( $ i == ( $ l + 1 ) ) return false ; $ i = 2 ; for ( $ i = 2 ; $ i <= $ l ; $ i ++ ) { if ( isPalindrome ( substr ( $ s , $ l - $ i , $ i ) ) ) return true ; } return false ; } $ s = " abccbarfgdbd " ; if ( CheckStr ( $ s ) ) echo ( " YES STRNEWLINE " ) ; else echo ( " NO STRNEWLINE " ) ; ? >