Check given string is oddly palindrome or not | Set 2 | C ++ program for the above approach ; Function to check if string formed by odd indices is palindromic or not ; Check if length of OddString odd , to consider edge case ; Push odd index character of first half of str in stack ; Middle element of odd length palindromic string is not compared ; If stack is empty then return true ; Driver Code ; Given string
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isOddStringPalindrome ( string str , int n ) { int oddStringSize = n / 2 ; bool lengthOdd = ( ( oddStringSize % 2 == 1 ) ? true : false ) ; stack < char > s ; int i = 1 ; int c = 0 ; while ( i < n && c < oddStringSize / 2 ) { s . push ( str [ i ] ) ; i += 2 ; c ++ ; } if ( lengthOdd ) i = i + 2 ; while ( i < n && s . size ( ) > 0 ) { if ( s . top ( ) == str [ i ] ) s . pop ( ) ; else break ; i = i + 2 ; } if ( s . size ( ) == 0 ) return true ; return false ; } int main ( ) { int N = 10 ; string s = " aeafacafae " ; if ( isOddStringPalindrome ( s , N ) ) cout << " Yes STRNEWLINE " ; else cout << " No STRNEWLINE " ; return 0 ; }