Modify string by rearranging vowels in alphabetical order at their respective indices | Java program for the above approach ; Function to arrange the vowels in sorted order in the string at their respective places ; Store the size of the string ; Stores vowels of string S ; Traverse the string , S and push all the vowels to string vow ; If vow is empty , then print S and return ; Convert vow to char array ; Sort vow in alphabetical order ; Traverse the string , S ; Replace S [ i ] with vow [ j ] iif S [ i ] is a vowel , and increment j by 1 ; Print the string ; Driver Code
import java . util . Arrays ; class GFG { static void sortVowels ( String S ) { int n = S . length ( ) ; String vow = " " ; for ( int i = 0 ; i < n ; i ++ ) { if ( S . charAt ( i ) == ' a ' || S . charAt ( i ) == ' e ' || S . charAt ( i ) == ' i ' || S . charAt ( i ) == ' o ' || S . charAt ( i ) == ' u ' ) { vow = vow . substring ( 0 , vow . length ( ) ) + S . charAt ( i ) ; } } if ( vow . length ( ) == 0 ) { System . out . print ( S ) ; return ; } char tempArray [ ] = vow . toCharArray ( ) ; Arrays . sort ( tempArray ) ; int j = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( S . charAt ( i ) == ' a ' || S . charAt ( i ) == ' e ' || S . charAt ( i ) == ' i ' || S . charAt ( i ) == ' o ' || S . charAt ( i ) == ' u ' ) { S = S . substring ( 0 , i ) + tempArray [ j ++ ] + S . substring ( i + 1 , n ) ; } } System . out . print ( S ) ; } public static void main ( String [ ] args ) { String S = " geeksforgeeks " ; sortVowels ( S ) ; } }