Sort an alphanumeric string such that the positions of alphabets and numbers remain unchanged | C ++ implementation of the approach ; Function that returns the string s in sorted form such that the positions of alphabets and numeric digits remain unchanged ; String to character array ; Sort the array ; Count of alphabets and numbers ; Get the index from where the alphabets start ; Now replace the string with sorted string ; If the position was occupied by an alphabet then replace it with alphabet ; Else replace it with a number ; Return the sorted string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string sort ( string s ) { char c [ s . length ( ) + 1 ] ; strcpy ( c , s . c_str ( ) ) ; sort ( c , c + s . length ( ) ) ; int al_c = 0 , nu_c = 0 ; while ( c [ al_c ] < 97 ) al_c ++ ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s [ i ] < 97 ) s [ i ] = c [ nu_c ++ ] ; else s [ i ] = c [ al_c ++ ] ; } return s ; } int main ( ) { string s = " d4c3b2a1" ; cout << sort ( s ) ; return 0 ; }