Check if both halves of a string are Palindrome or not | C ++ Program to check whether both halves of a string is Palindrome or not ; Function to check if both halves of a string are palindrome or not ; Length of string ; Initialize both part as true ; If first half is not palindrome ; If second half is not palindrome ; If both halves are Palindrome ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void checkPalindrome ( string S ) { int N = S . size ( ) ; bool first_half = true ; bool second_half = true ; int cnt = ( N / 2 ) - 1 ; for ( int i = 0 ; i < ( ( N / 2 ) / 2 ) ; i ++ ) { if ( S [ i ] != S [ cnt ] ) { first_half = false ; break ; } if ( S [ N / 2 + i ] != S [ N / 2 + cnt ] ) { second_half - false ; break ; } cnt -- ; } if ( first_half && second_half ) { cout << " Yes " << endl ; } else { cout << " No " << endl ; } } int main ( ) { string S = " momdad " ; checkPalindrome ( S ) ; return 0 ; }