Replace ' ? ' in a string such that no two adjacent characters are same | C ++ program for the above approach ; Function that replace all ' ? ' with lowercase alphabets such that each adjacent character is different ; Store the given string ; If the first character is ' ? ' ; Traverse the string [ 1 , N - 1 ] ; If the current character is ' ? ' ; Change the character ; Check equality with the previous character ; Check equality with the next character ; Check equality with the previous character ; If the last character is ' ? ' ; Change character ; Check with previous character ; Return the resultant string ; Driver Code ; Given string S ; Function Call
#include " bits / stdc + + . h " NEW_LINE using namespace std ; string changeString ( string S ) { string s = S ; int N = ( int ) s . length ( ) ; if ( s [ 0 ] == ' ? ' ) { s [ 0 ] = ' a ' ; if ( s [ 0 ] == s [ 1 ] ) { s [ 0 ] ++ ; } } for ( int i = 1 ; i < N - 1 ; i ++ ) { if ( s [ i ] == ' ? ' ) { s [ i ] = ' a ' ; if ( s [ i ] == s [ i - 1 ] ) { s [ i ] ++ ; } if ( s [ i ] == s [ i + 1 ] ) { s [ i ] ++ ; } if ( s [ i ] == s [ i - 1 ] ) { s [ i ] ++ ; } } } if ( s [ N - 1 ] == ' ? ' ) { s [ N - 1 ] = ' a ' ; if ( s [ N - 1 ] == s [ N - 2 ] ) { s [ N - 1 ] ++ ; } } return s ; } int main ( ) { string S = " ? a ? a " ; cout << changeString ( S ) ; return 0 ; }