| xHarbour Reference Documentation > Function Reference |
![]() |
![]() |
![]() |
Creates a histogram of characters in a character string
CharHist( [<cString>] ) --> aHistogram
The function returns a one dimensional array of 256 elements. Each element hold a numeric value which indicates how often a character is found in <cString>. The numeric ASCII code of a character plus 1 points to the array element holding the result for a single character.
| See also: | CharList(), CharNoList(), CharSList(), CharSort() |
| Category: | CT:String manipulation , Character functions , xHarbour extensions |
| Source: | ct\misc1.c |
| LIB: | xhb.lib |
| DLL: | xhbdll.dll |
// The example creates a histogram of a character string
// and displays which character appears how often.
PROCEDURE Main
LOCAL cString := "The excellent xHarbour compiler"
LOCAL aHist, cChar, nCount
aHist := CharHist( cString )
// remove duplicate characters and sort
cString := CharSList( cString )
// display the histogram
FOR EACH cChar IN cString
// the ASCII code of a character points to the histogram array
nCount := aHist[ Asc(cChar)+1 ]
IF nCount == 1
? '"' + cChar + '" = 1x'
ELSEIF nCount > 0
? '"' + cChar + '" = ' + LTrim(Str(nCount)) +"x"
ENDIF
NEXT
/* Result:
" " = 3x
"H" = 1x
"T" = 1x
"a" = 1x
"b" = 1x
"c" = 2x
"e" = 5x
"h" = 1x
"i" = 1x
"l" = 3x
"m" = 1x
"n" = 1x
"o" = 2x
"p" = 1x
"r" = 3x
"t" = 1x
"u" = 1x
"x" = 2x
*/
RETURN
http://www.xHarbour.com