Replace all consonants with nearest vowels in a string | C ++ program to replace all consonants with nearest vowels in a string ; Function to check if a character is vowel or not ; Function to replace consonant with nearest vowels ; if , string element is vowel , jump to next element ; check if consonant lies between two vowels , if it lies , than replace it with nearest vowel ; here the absolute difference of ascii value is considered ; when s [ i ] is equal to either ' v ' , ' w ' , ' x ' , ' y ' , ' z ' ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isVowel ( char ch ) { if ( ch != ' a ' && ch != ' e ' && ch != ' i ' && ch != ' o ' && ch != ' u ' ) return false ; return true ; } string replacingConsonants ( string s ) { for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( isVowel ( s [ i ] ) ) continue ; else { if ( s [ i ] > ' a ' && s [ i ] < ' e ' ) { if ( abs ( s [ i ] - ' a ' ) > abs ( s [ i ] - ' e ' ) ) s [ i ] = ' e ' ; else s [ i ] = ' a ' ; } else if ( s [ i ] > ' e ' && s [ i ] < ' i ' ) { if ( abs ( s [ i ] - ' e ' ) > abs ( s [ i ] - ' i ' ) ) s [ i ] = ' i ' ; else s [ i ] = ' e ' ; } else if ( s [ i ] > ' i ' && s [ i ] < ' o ' ) { if ( abs ( s [ i ] - ' i ' ) > abs ( s [ i ] - ' o ' ) ) s [ i ] = ' o ' ; else s [ i ] = ' i ' ; } else if ( s [ i ] > ' o ' && s [ i ] < ' u ' ) { if ( abs ( s [ i ] - ' o ' ) > abs ( s [ i ] - ' u ' ) ) s [ i ] = ' u ' ; else s [ i ] = ' o ' ; } else if ( s [ i ] > ' u ' ) s [ i ] = ' u ' ; } } return s ; } int main ( ) { string s = " geeksforgeeks " ; cout << replacingConsonants ( s ) ; return 0 ; }