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
function isVowel ( ch ) { if ( ch != ' ' && ch != ' ' && ch != ' ' && ch != ' ' && ch != ' ' ) return false ; return true ; } function replaceConsonants ( s ) { for ( var i = 0 ; i < s . length ; i ++ ) { if ( ! isVowel ( s [ i ] ) ) { if ( s [ i ] == ' ' ) s [ i ] = ' ' ; else { s [ i ] = String . fromCharCode ( s [ i ] . charCodeAt ( 0 ) + 1 ) ; if ( isVowel ( s [ i ] ) ) s [ i ] = String . fromCharCode ( s [ i ] . charCodeAt ( 0 ) + 1 ) ; } } } return s . join ( ' ' ) ; } var s = " " . split ( ' ' ) ; document . write ( replaceConsonants ( s ) ) ;