Replace consonants with next immediate consonants alphabetically in a String | Function to check if a character is vowel or not ; Function that replaces consonant with next immediate consonant alphabatically ; Start traversing the string ; if character is z , than replace it with character b ; if the alphabet is not z ; replace the element with next immediate alphabet ; if next immediate alphabet is vowel , than take next 2 nd immediate alphabet ( since no two vowels occurs consecutively in alphabets ) hence no further checking is required ; Driver code
< ? php function isVowel ( $ ch ) { if ( $ ch != ' a ' && $ ch != ' e ' && $ ch != ' i ' && $ ch != ' o ' && $ ch != ' u ' ) return false ; return true ; } function replaceConsonants ( $ s ) { for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) { if ( ! isVowel ( $ s [ $ i ] ) ) { if ( $ s [ $ i ] == ' z ' ) $ s [ $ i ] = ' b ' ; else { $ s [ $ i ] = chr ( ord ( $ s [ $ i ] ) + 1 ) ; if ( isVowel ( $ s [ $ i ] ) ) $ s [ $ i ] = chr ( ord ( $ s [ $ i ] ) + 1 ) ; } } } return $ s ; } $ s = " geeksforgeeks " ; echo replaceConsonants ( $ s ) ; ? >