Replace all consonants with nearest vowels in a string | Java 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 bsolute difference of ascii value is considered ; when s . charAt ( i ) is equal to either ' v ' , ' w ' , ' x ' , ' y ' , ' z ' ; Driver code
import java . util . * ; class Solution { static boolean isVowel ( char ch ) { if ( ch != ' a ' && ch != ' e ' && ch != ' i ' && ch != ' o ' && ch != ' u ' ) return false ; return true ; } static String replacingConsonants ( String s ) { for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( isVowel ( s . charAt ( i ) ) ) continue ; else { if ( s . charAt ( i ) > ' a ' && s . charAt ( i ) < ' e ' ) { if ( Math . abs ( s . charAt ( i ) - ' a ' ) > Math . abs ( s . charAt ( i ) - ' e ' ) ) s = s . substring ( 0 , i ) + ' e ' + s . substring ( i + 1 ) ; else s = s . substring ( 0 , i ) + ' a ' + s . substring ( i + 1 ) ; } else if ( s . charAt ( i ) > ' e ' && s . charAt ( i ) < ' i ' ) { if ( Math . abs ( s . charAt ( i ) - ' e ' ) > Math . abs ( s . charAt ( i ) - ' i ' ) ) s = s . substring ( 0 , i ) + ' i ' + s . substring ( i + 1 ) ; else s = s . substring ( 0 , i ) + ' e ' + s . substring ( i + 1 ) ; } else if ( s . charAt ( i ) > ' i ' && s . charAt ( i ) < ' o ' ) { if ( Math . abs ( s . charAt ( i ) - ' i ' ) > Math . abs ( s . charAt ( i ) - ' o ' ) ) s = s . substring ( 0 , i ) + ' o ' + s . substring ( i + 1 ) ; else s = s . substring ( 0 , i ) + ' i ' + s . substring ( i + 1 ) ; } else if ( s . charAt ( i ) > ' o ' && s . charAt ( i ) < ' u ' ) { if ( Math . abs ( s . charAt ( i ) - ' o ' ) > Math . abs ( s . charAt ( i ) - ' u ' ) ) s = s . substring ( 0 , i ) + ' u ' + s . substring ( i + 1 ) ; else s = s . substring ( 0 , i ) + ' o ' + s . substring ( i + 1 ) ; } else if ( s . charAt ( i ) > ' u ' ) s = s . substring ( 0 , i ) + ' u ' + s . substring ( i + 1 ) ; } } return s ; } public static void main ( String args [ ] ) { String s = " geeksforgeeks " ; System . out . print ( replacingConsonants ( s ) ) ; } }