Sort a string without altering the position of vowels

Last Updated :

Given a string S of size N, the task is to sort the string without changing the place of vowels.

Examples:

Input: S = "example"
Output: elampxe
Explanation:
The consonants are xmpl. After sorting it becomes lmpx.
Now update the string after replacing each consonants with sorted consonants.

Input: S = "Cplusplus"
Output: Clluppsus

Recommended: Selection Sort

Approach:

To solve the above problem, follow the steps below:

  • Traverse the string S, copy all consonants of string S to another string say, temp.
  • Sort temp in alphabetical order.
  • Now again traverse the string S, and replace each consonant of string S with the temp[i], and increment i.

Below is the implementation of the above approach:

// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;

// Function to sort the string without
// changing the place of vowels
void sortStr(string S)
{
    // Length of string S
    int N = S.size();

    string temp = "";
    // Traverse the string S
    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];
    }

    // Sort string temp
    if (temp.size())
        sort(temp.begin(), temp.end());

    // Pointing to starting index of temp
    int ptr = 0;

    // Traverse the string S
    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;
}

// Driver Code
int main()
{

    string S = "example";
    sortStr(S);
    return 0;
}

Output:

elampxe

Time Complexity: O(nlogn)
Auxiliary Space: O(n)

Please write comments if you find anything incorrect. A gentle request to share this topic on your social media profile.

Comment
Next Article
Mithlesh Upadhyay Published 02 Oct, 2023 · 2 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Similar Reads

  • Find the Nth Triacontakaihenagonal Number

    Given a number N, the task is to find Nth triacontakaihenagonal number. A triacontakaihenagonal number is class of figurate number. It has 31 – sided polygon called triacontakaihenagon.…

    1 min read
  • Check if a Number is a Tetracontagon Number

    Given an integer N, the task is to check if it is a Tetracontagon number or not. Tetracontagon number is class of figurate number. It has 40 –…

    2 min read
  • Find max rational number in given Array

    Given the Numerator and Denominator of N Rational Numbers, the task is to find the maximum Rational Number from the array. Examples: Input: N = 2 num[] =…

    2 min read
  • Check if a Number is Pentacontagon

    Given an integer N, the task is to check if it is a pentacontagon number or not. Pentacontagon number is class of figurate number. It has 50 –…

    2 min read
  • Check if a Number is Triacontakaidigon

    Given an integer N, the task is to check if it is a Triacontakaidigon number or not. Triacontakaidigon number is class of figurate number. It has 32 –…

    2 min read
  • Check if a Number is Hexacontagon

    Given an integer N, the task is to check if it is a hexacontagon number or not. Hexacontagon number is class of figurate number. It has 60 –…

    2 min read