Group consecutive characters of same type in a string | C # implementation of the approach ; Function to return the modified String ; Store original String ; Remove all white spaces ; To store the resultant String ; Traverse the String ; Group upper case characters ; Group numeric characters ; Group arithmetic operators ; Return the resultant String ; Driver code
using System ; class GFG { static String groupCharacters ( char [ ] s , int len ) { string temp = " " ; for ( int j = 0 ; j < len ; j ++ ) if ( s [ j ] != ' ▁ ' ) temp = temp + s [ j ] ; len = temp . Length ; string ans = " " ; int i = 0 ; while ( i < len ) { if ( temp [ i ] >= ( ' A ' ) && temp [ i ] <= ( ' Z ' ) ) { while ( i < len && temp [ i ] >= ( ' A ' ) && temp [ i ] <= ( ' Z ' ) ) { ans = ans + temp [ i ] ; i ++ ; } ans = ans + " ▁ " ; } else if ( temp [ i ] >= ( '0' ) && temp [ i ] <= ( '9' ) ) { while ( i < len && temp [ i ] >= ( '0' ) && temp [ i ] <= ( '9' ) ) { ans = ans + temp [ i ] ; i ++ ; } ans = ans + " ▁ " ; } else { while ( i < len && temp [ i ] >= ( ' * ' ) && temp [ i ] <= ( ' / ' ) ) { ans = ans + temp [ i ] ; i ++ ; } ans = ans + " ▁ " ; } } return ans ; } public static void Main ( ) { string s = "34FTG234 + ▁ + - ▁ * " ; int len = s . Length ; Console . WriteLine ( groupCharacters ( s . ToCharArray ( ) , len ) ) ; } }