Sort a string without altering the position of vowels | Java program for the above approach ; Function to sort the string leaving the vowels unchanged ; Length of string S ; Traverse the string S ; Sort the string temp ; Pointer to traverse the sorted string of consonants ; Traverse the string S ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void sortStr ( String str ) { char S [ ] = str . toCharArray ( ) ; int N = S . length ; ArrayList < Character > temp = new ArrayList < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] != ' a ' && S [ i ] != ' e ' && S [ i ] != ' i ' && S [ i ] != ' o ' && S [ i ] != ' u ' ) temp . add ( S [ i ] ) ; } if ( temp . size ( ) != 0 ) Collections . sort ( temp ) ; int ptr = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] != ' a ' && S [ i ] != ' e ' && S [ i ] != ' i ' && S [ i ] != ' o ' && S [ i ] != ' u ' ) S [ i ] = temp . get ( ptr ++ ) ; } System . out . println ( new String ( S ) ) ; } public static void main ( String [ ] args ) { String S = " geeksforgeeks " ; sortStr ( S ) ; } }