Rearrange a string in sorted order followed by the integer sum | PHP program for above implementation ; Function to return string in lexicographic order followed by integers sum ; Traverse the string ; Count occurrence of uppercase alphabets ; Store sum of integers ; Traverse for all characters A to Z ; Append the current character in the string no . of times it occurs in the given string ; Append the sum of integers ; return resultant string ; Driver Code
< ? php $ MAX_CHAR = 26 ; function arrangeString ( $ str ) { global $ MAX_CHAR ; $ char_count = array_fill ( 0 , $ MAX_CHAR , NULL ) ; $ sum = 0 ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) { if ( $ str [ $ i ] >= ' A ' && $ str [ $ i ] <= ' Z ' ) $ char_count [ ord ( $ str [ $ i ] ) - ord ( ' A ' ) ] ++ ; else $ sum = $ sum + ( ord ( $ str [ $ i ] ) - ord ( '0' ) ) ; } $ res = " " ; for ( $ i = 0 ; $ i < $ MAX_CHAR ; $ i ++ ) { $ ch = chr ( ord ( ' A ' ) + $ i ) ; while ( $ char_count [ $ i ] -- ) $ res = $ res . $ ch ; } if ( $ sum > 0 ) $ res = $ res . strval ( $ sum ) ; return $ res ; } $ str = " ACCBA10D2EW30" ; echo arrangeString ( $ str ) ; ? >