Group consecutive characters of same type in a string | 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
function groupCharacters ( s , len ) { let temp = " " ; for ( let i = 0 ; i < len ; i ++ ) if ( s [ i ] != ' ' ) temp = temp + s [ i ] ; len = temp . length ; let ans = " " ; let i = 0 ; while ( i < len ) { if ( temp [ i ] >= ( ' ' ) && temp [ i ] <= ( ' ' ) ) { while ( i < len && temp [ i ] >= ( ' ' ) && temp [ i ] <= ( ' ' ) ) { ans = ans + temp [ i ] ; i ++ ; } ans = ans + " " ; } else if ( temp [ i ] >= ( ' ' ) && temp [ i ] <= ( ' ' ) ) { while ( i < len && temp [ i ] >= ( ' ' ) && temp [ i ] <= ( ' ' ) ) { ans = ans + temp [ i ] ; i ++ ; } ans = ans + " " ; } else { while ( i < len && temp [ i ] >= ( ' ' ) && temp [ i ] <= ( ' ' ) ) { ans = ans + temp [ i ] ; i ++ ; } ans = ans + " " ; } } return ans ; } let s = " " ; let len = s . length ; document . write ( groupCharacters ( s . split ( " " ) , len ) ) ;