Count the number of words having sum of ASCII values less than and greater than k | Function to count the words ; Sum of ascii values ; Number of words having sum of ascii less than k ; If character is a space ; Add the ascii value to sum ; Handling the Last word separately ; Driver code
< ? php function CountWords ( $ str , $ k ) { $ sum = 0 ; $ NumberOfWords = 0 ; $ counter = 0 ; $ len = strlen ( $ str ) ; for ( $ i = 0 ; $ i < $ len ; ++ $ i ) { if ( $ str [ $ i ] == ' ▁ ' ) { if ( $ sum < $ k ) $ counter ++ ; $ sum = 0 ; $ NumberOfWords ++ ; } else $ sum += ord ( $ str [ $ i ] ) ; } $ NumberOfWords ++ ; if ( $ sum < $ k ) $ counter ++ ; echo " Number ▁ of ▁ words ▁ having ▁ sum ▁ of ▁ ASCII " . " values less than k = " ▁ . ▁ $ counter ▁ . ▁ " " ; STRNEWLINE TABSYMBOL echo ▁ " Number of words having sum of ASCII " ▁ . STRNEWLINE TABSYMBOL TABSYMBOL " values greater than or equal to k = " ( $ NumberOfWords - $ counter ) ; } $ str = " Learn ▁ how ▁ to ▁ code " ; $ k = 400 ; CountWords ( $ str , $ k ) ; ? >