Saturday, 23 Oct 2021

# Sort a string without altering the position of vowels

Sort a string without altering the position of vowels
Given a string S of size N, the task is to sort the string without changing the position of vowels.
Examples:

Input: S = “geeksforgeeks”Output: feeggkokreessExplanation:The consonants present in the string are gksfrgks. Sorting the consonants modifies their sequence to fggkkrss.Now, update the string by placing the sorted consonants in those positions.
Input: S = “apple”Output: alppe

Approach: Follow the steps below to solve the problem:Below is the implementation of the above approach:

C++

#include
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