file$ = "C:\BlueJ\examples\shapes\README.TXT"
open to read 1, file$
 
print wordCount(1,1)
 
close file 1
suspend for key
end
 
 
REM nFile = file number to scan
REM r = what data to return from file:
REM 1 = word count
REM 2 = character count with spaces
REM 3 = character count without spaces
function wordCount(nFile, r)
   rem only counts words with more than this many characters
   minChars = 1
   wCount = 0
   charCount = 0
   spaces = 0
   while file end(nFile) = 0
      read string nFile, s$
      charCount = charCount + len(s$)
      start = 1
      for i=1 to len(s$)
         if mid$(s$, i) = " "
            inc spaces
            if i-start > minChars then inc wCount
            start = i
         endif
      next i
      rem end of line word check check
      if i-start > minChars then inc wCount
   endwhile
   charCountNoSpaces = charcount-spaces
   if r = 1 then exitfunction wCount
   if r = 2 then exitfunction charCount
   if r = 3 then exitfunction charCountNoSpaces
endfunction -1