Sort a string without altering the position of vowels

Last Updated : 23 Jul, 2025

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: feeggkokreess
Explanation:
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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;

// Function to sort the string
// leaving the vowels unchanged
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 the string temp
    if (temp.size())
        sort(temp.begin(), temp.end());

    // Pointer to traverse the
    // sorted string of consonants
    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 = "geeksforgeeks";
    sortStr(S);
    return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;

class GFG{

// Function to sort the string
// leaving the vowels unchanged
static void sortStr(String str)
{
    char S[] = str.toCharArray();

    // Length of string S
    int N = S.length;

    ArrayList<Character> temp = new ArrayList<>();

    // 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.add(S[i]);
    }

    // Sort the string temp
    if (temp.size() != 0)
        Collections.sort(temp);

    // Pointer to traverse the
    // sorted string of consonants
    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.get(ptr++);
    }
    System.out.println(new String(S));
}

// Driver Code
public static void main(String[] args)
{
    String S = "geeksforgeeks";
    
    sortStr(S);
}
}

// This code is contributed by Kingash
Python3
# Python3 program for the above approach

# Function to sort the string
# leaving the vowels unchanged
def sortStr(S):

    # Length of string S
    N = len(S)
    temp = ""

    # Traverse the string S
    for i in range(N):
        if (S[i] != 'a' and S[i] != 'e' and S[i] != 'i'
                and S[i] != 'o'and S[i] != 'u'):
            temp += S[i]

    # Sort the string temp
    if (len(temp)):
        p = list(temp)
        p.sort()
        temp=''.join(p)
        
    # Pointer to traverse the
    # sorted string of consonants
    ptr = 0

    # Traverse the string S
    for i in range(N):
      S = list(S)
      if (S[i] != 'a' and S[i] != 'e' and S[i] != 'i'
                and S[i] != 'o' and S[i] != 'u'):
            S[i] = temp[ptr]
            ptr += 1

    print(''.join(S))

# Driver Code
if __name__ == "__main__":

    S = "geeksforgeeks"
    sortStr(S)

    # This code is contributed by ukasp.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG{

// Function to sort the string
// leaving the vowels unchanged
static void sortStr(String str)
{
    char []S = str.ToCharArray();

    // Length of string S
    int N = S.Length;

    List<char> temp = new List<char>();

    // 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.Add(S[i]);
    }

    // Sort the string temp
    if (temp.Count != 0)
        temp.Sort();

    // Pointer to traverse the
    // sorted string of consonants
    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++];
    }
    Console.WriteLine(new String(S));
}

// Driver Code
public static void Main(String[] args)
{
    String S = "geeksforgeeks";
    
    sortStr(S);
}
}

// This code is contributed by Amit Katiyar 
JavaScript
<script>

// JavaScript program for the above approach

// Function to sort the string
// leaving the vowels unchanged
function sortStr(str)
{
    var S = str.split('');

    // Length of string S
    var N = S.length;

    var temp = [];

    // Traverse the string S
    for(var i = 0; i < N; i++)
    {
        if (S[i] != 'a' && S[i] != 'e' && 
            S[i] != 'i' && S[i] != 'o' &&
            S[i] != 'u')
            temp.push(S[i]);
    }

    // Sort the string temp
    if (temp.length != 0)
        temp.sort();

    // Pointer to traverse the
    // sorted string of consonants
    var ptr = 0;

    // Traverse the string S
    for(var 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++];
    }
    var str = "";
    for(var i =0;i<S.length;i++)
    str+=S[i];
    
    document.write(str);
}

// Driver Code


    var S = "geeksforgeeks";
    
    sortStr(S);

// This code is contributed by Amit Katiyar

</script>

Output: 
feeggkokreess

 

Time Complexity: O(N logN)
Auxiliary Space: O(N)


 

Comment