;+ ; ; @file_comments ; Return the number of words in the given text string. ; ; @categories ; ; @param TXTSTR ; = text string to examine. ; ; @keyword DELIMITER ; = d. Set delimiter character (def = space). ; ; @keyword HELP ; ; @returns ; n = number of words found. ; ; @restrictions ; See also getwrd. ; ; @history ; R. Sterner, 7 Feb, 1985. ; Johns Hopkins University Applied Physics Laboratory. ; RES 4 Sep, 1989 --- converted to SUN. ; ; Copyright (C) 1985, Johns Hopkins University/Applied Physics Laboratory ; This software may be used, copied, or redistributed as long as it is not ; sold and this copyright notice is reproduced on each copy made. This ; routine is provided as is without any express or implied warranties ; whatsoever. Other limitations apply as described in the file disclaimer.txt. ; ; @version ; $Id$ ; ;- ; FUNCTION nwrds, txtstr, HELP=hlp, DELIMITER=delim ; compile_opt idl2, strictarrsubs ; if (n_params(0) lt 1) or keyword_set(hlp) then begin print,' Return the number of words in the given text string.' print,' n = nwrds(txt)' print,' txt = text string to examine. in' print,' n = number of words found. out' print,' Keywords:' print,' DELIMITER = d. Set delimiter character (def = space).' print,' Notes: See also getwrd.' return, -1 endif if strlen(txtstr) eq 0 then return,0 ; A null string has 0 words. ddel = ' ' ; Default word delimiter is a space. if n_elements(delim) ne 0 then ddel = delim ; Use given word delimiter. tst = (byte(ddel))[0] ; Delimiter as a byte value. tb = byte(txtstr) ; String to bytes. if ddel eq ' ' then begin ; Check for tabs? w = where(tb eq 9B, cnt) ; Yes. if cnt gt 0 then tb[w] = 32B ; Convert any to space. endif x = tb ne tst ; Locate words. x = [0,x,0] ; Pad ends with delimiters. y = (x-shift(x,1)) eq 1 ; Look for word beginnings. n = fix(total(y)) ; Count word beginnings. return, n end