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
using System ; class GFG { static string sort ( string s ) { char [ ] c = new char [ s . Length + 1 ] ; c = s . ToCharArray ( ) ; Array . Sort ( c ) ; 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 = s . Substring ( 0 , i ) + c [ nu_c ++ ] + s . Substring ( i + 1 ) ; else s = s . Substring ( 0 , i ) + c [ al_c ++ ] + s . Substring ( i + 1 ) ; } return s ; } public static void Main ( ) { string s = " d4c3b2a1" ; Console . WriteLine ( sort ( s ) ) ; } }