xHarbour Reference Documentation > Function Reference xHarbour Developers Network  

CharHist()

Creates a histogram of characters in a character string

Syntax

CharHist( [<cString>] ) --> aHistogram

Arguments

<cString>
This is the character string to process. It defaults to a null string ("").

Return

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.

Info

See also:CharList(), CharNoList(), CharSList(), CharSort()
Category: CT:String manipulation , Character functions , xHarbour extensions
Source:ct\misc1.c
LIB:xhb.lib
DLL:xhbdll.dll

Example

// 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

Copyright © 2006-2007 xHarbour.com Inc. All rights reserved.
http://www.xHarbour.com
Created by docmaker.exe