Case-specific Sorting of Strings - Codeprg

Breaking

programing News Travel Computer Engineering Science Blogging Earning

Saturday, 1 August 2020

Case-specific Sorting of Strings

Case-specific Sorting of Strings


Case-specific Sorting of Strings
Case-specific Sorting of Strings


Given a string S consisting of uppercase and lowercase characters. The task is to sort uppercase and lowercase letters 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.



This is a function problem. You only need to complete the function caseSort that returns sorted string.


Input:
2
12
defRTSersUXI
6
srbDKi

Output:
deeIRSfrsTUX
birDKs


// { Driver Code Starts
#include<bits/stdc++.h>
using namespace std;


 // } Driver Code Ends


string caseSort(string str, int n){
   
    // your code here
    string s1="",s2="",s="";
    for(int i=0;i<n;++i)
    {
        if(str[i]>='a'&&str[i]<='z')
        {
            s1+=str[i];
        }else{
            s2+=str[i];
        }
    }
   
    sort(s1.begin(),s1.end());
    sort(s2.begin(),s2.end());
    int j=0,k=0;
    for(int i=0;i<n;++i)
    {
        if(str[i]>='a'&&str[i]<='z')
        {
            s+=s1[j++];
        }else{
            s+=s2[k++];
        }
    }
    return s;
       
}

// { Driver Code Starts.

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        string str;
        cin>>str;
       
        cout<<caseSort (str, n)<<endl;
    }
}  // } Driver Code Ends