Sort a string without altering the position of vowels | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sortStr ( string S ) { int N = S . size ( ) ; string temp = " " ; for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] != ' a ' && S [ i ] != ' e ' && S [ i ] != ' i ' && S [ i ] != ' o ' && S [ i ] != ' u ' ) temp += S [ i ] ; } if ( temp . size ( ) ) sort ( temp . begin ( ) , temp . end ( ) ) ; 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 [ ptr ++ ] ; } cout << S ; } int main ( ) { string S = " geeksforgeeks " ; sortStr ( S ) ; return 0 ; }