Modify string by rearranging vowels in alphabetical order at their respective indices | C ++ 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 ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sortVowels ( string S ) { int n = S . size ( ) ; string vow = " " ; for ( int i = 0 ; i < n ; i ++ ) { if ( S [ i ] == ' a ' S [ i ] == ' e ' S [ i ] == ' i ' S [ i ] == ' o ' S [ i ] == ' u ' ) { vow += S [ i ] ; } } if ( vow . size ( ) == 0 ) { cout << S ; return ; } sort ( vow . begin ( ) , vow . end ( ) ) ; int j = 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 ] = vow [ j ++ ] ; } } cout << S ; } int main ( ) { string S = " geeksforgeeks " ; sortVowels ( S ) ; return 0 ; }