Check if the string has a reversible equal substring at the ends | C ++ program for the above approach ; Function to print longest substring that appears at beginning of string and also at end in reverse order ; Stores the resultant string ; If the characters are same ; Otherwise , break ; If the string can 't be formed ; Otherwise print resultant string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void commonSubstring ( string s ) { int n = s . size ( ) ; int i = 0 ; int j = n - 1 ; string ans = " " ; while ( j >= 0 ) { if ( s [ i ] == s [ j ] ) { ans += s [ i ] ; i ++ ; j -- ; } else { break ; } } if ( ans . size ( ) == 0 ) cout << " False " ; else { cout << " True ▁ STRNEWLINE " << ans ; } } int main ( ) { string S = " abca " ; commonSubstring ( S ) ; return 0 ; }