Sort groups of numbers and characters separately in a String

Given a string str of alphanumeric characters, the task is to sort the similar group of consecutive characters separately and print the modified string i.e. all consecutive groups of digits and alphabetical characters will be sorted separately.

Examples:

Input: str = “121geeks21”
Output: 112eegks12
“121”, “geeks”, “21” are the valid groups
and they will be sorted separately.



Input: str = “cba321ab”
Output: abc123ab

Approach: Create a vector to store the starting indices of all the valid groups in the given string. Now, traverse the string character by character and if the current character is from a different group than the previous character then push the current index to the vector. After the string has been traversed completely, sort all the individual groups of substring of the given string using the vector updated earlier. Finally, print the modified string.

Below is the implementation of the above approach:

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 modified string
string get_string(string str, int n)
{
  
    // To store the previous character
    char prev = str[0];
  
    // To store the starting indices
    // of all the groups
    vector<int> result;
  
    // Starting index of the first group
    result.push_back(0);
  
    for (int i = 1; i < n; i++) {
  
        // If the current character and the
        // previous character differ
        if (isdigit(str[i]) != isdigit(prev)) {
  
            // Push the starting index
            // of the new group
            result.push_back(i);
        }
  
        // The current character now becomes previous
        prev = str[i];
    }
  
    // Sort the first group
    sort(str.begin(), str.begin() + result[0]);
  
    // Sort all the remaining groups
    for (int i = 0; i < result.size() - 1; i++) {
        sort(str.begin() + result[i],
             str.begin() + result[i + 1]);
    }
  
    // Sort the last group
    sort(str.begin() + result[result.size() - 1],
         str.end());
  
    // Return the modified string
    return str;
}
  
// Driver code
int main()
{
    string str = "121geeks21";
    int n = str.length();
  
    cout << get_string(str, n);
  
    return 0;
}

chevron_right


Output:

112eegks12



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.


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.