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
#include <bits/stdc++.h> NEW_LINE using namespace std ; string groupCharacters ( string s , int len ) { string temp = " " ; for ( int i = 0 ; i < len ; i ++ ) if ( s [ i ] != ' ▁ ' ) temp = temp + s [ i ] ; len = temp . length ( ) ; string ans = " " ; int i = 0 ; while ( i < len ) { if ( int ( temp [ i ] ) >= int ( ' A ' ) && int ( temp [ i ] ) <= int ( ' Z ' ) ) { while ( i < len && int ( temp [ i ] ) >= int ( ' A ' ) && int ( temp [ i ] ) <= int ( ' Z ' ) ) { ans = ans + temp [ i ] ; i ++ ; } ans = ans + " ▁ " ; } else if ( int ( temp [ i ] ) >= int ( '0' ) && int ( temp [ i ] ) <= int ( '9' ) ) { while ( i < len && int ( temp [ i ] ) >= int ( '0' ) && int ( temp [ i ] ) <= int ( '9' ) ) { ans = ans + temp [ i ] ; i ++ ; } ans = ans + " ▁ " ; } else { while ( i < len && int ( temp [ i ] ) >= int ( ' * ' ) && int ( temp [ i ] ) <= int ( ' / ' ) ) { ans = ans + temp [ i ] ; i ++ ; } ans = ans + " ▁ " ; } } return ans ; } int main ( ) { string s = "34FTG234 + ▁ + - ▁ * " ; int len = s . length ( ) ; cout << groupCharacters ( s , len ) ; return 0 ; }