Case-specific Sorting of Strings

Given a string str consisting of uppercase and lowercase characters. The task is to sort uppercase and lowercase characters separately such that if the ith place in the original string had an uppercase character then it should not have a lowercase character after being sorted and vice versa.

Examples:

Input: str = “gEeksfOrgEEkS”
Output: eEfggkEkrEOsS

Input: str = “eDefSR”
Output: eDefRS

Approach: The idea is simple to store lower case characters and upper case characters in two different vectors and sort both of the vectors. Then use the sorted vectors to get the sorted string.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the sorted string
string getSortedString(string s, int n)
{
  
    // Vectors to store the lowercase
    // and uppercase characters
    vector<char> v1, v2;
    for (int i = 0; i < n; i++) {
        if (s[i] >= 'a' && s[i] <= 'z')
            v1.push_back(s[i]);
        if (s[i] >= 'A' && s[i] <= 'Z')
            v2.push_back(s[i]);
    }
  
    // Sort both the vectors
    sort(v1.begin(), v1.end());
    sort(v2.begin(), v2.end());
    int i = 0, j = 0;
    for (int k = 0; k < n; k++) {
  
        // If current character is lowercase
        // then pick the lowercase character
        // from the sorted list
        if (s[k] >= 'a' && s[k] <= 'z') {
            s[k] = v1[i];
            ++i;
        }
  
        // Else pick the uppercase character
        else if (s[k] >= 'A' && s[k] <= 'Z') {
            s[k] = v2[j];
            ++j;
        }
    }
  
    // Return the sorted string
    return s;
}
  
// Driver code
int main()
{
    string s = "gEeksfOrgEEkS";
    int n = s.length();
  
    cout << getSortedString(s, n);
  
    return 0;
}

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
  
# Function to return the sorted string
def getSortedString(s, n):
  
    # Vectors to store the lowercase
    # and uppercase characters
    v1=[]
    v2=[]
    for i in range(n):
        if (s[i] >= 'a' and s[i] <= 'z'):
            v1.append(s[i])
        if (s[i] >= 'A' and s[i] <= 'Z'):
            v2.append(s[i])
  
    # Sort both the vectors
    v1=sorted(v1)
    v2=sorted(v2)
    i = 0
    j = 0
    for k in range(n):
  
        # If current character is lowercase
        # then pick the lowercase character
        # from the sorted list
        if (s[k] >= 'a' and s[k] <= 'z'):
            s[k] = v1[i]
            i+=1
  
        # Else pick the uppercase character
        elif (s[k] >= 'A' and s[k] <= 'Z'):
            s[k] = v2[j]
            j+=1
  
    # Return the sorted string
    return "".join(s)
  
  
# Driver code
s = "gEeksfOrgEEkS"
ss=[i for i in s]
n = len(ss)
  
print(getSortedString(ss, n))
  
# This code is contributed by mohit kumar 29

chevron_right


Output:

eEfggkEkrEOsS


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : mohit kumar 29



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.