home *** CD-ROM | disk | FTP | other *** search
/ Boston 2 / boston-2.iso / DOS / PROGRAM / BASIC / POWBASIC / LIBRARY1 / EXAMP1.ZIP / TWORD.BAS < prev    next >
BASIC Source File  |  1990-05-07  |  2KB  |  63 lines

  1. 'Test word
  2. 'Read a text file and count the number of words of length 1, 2,
  3. '3, and so on.  This program contains intentional bugs.  Use it
  4. 'in conjunction with the PowerBasic User's Guide to learn
  5. 'the PowerBasic integrated debugger.
  6. '
  7. MaxWordLen = 16    'count words up to a length of 16 characters
  8.                 'longer words will go into Overlong
  9. DIM WordLength(MaxWordLen)  'the array used to hold the counts
  10. Blank$ = CHR$(32)           'a space marks the end of a word.
  11.  
  12. PRINT "Warning: This is a program intended for use in a practice"
  13. PRINT "session of the PowerBasic debugger.  If you are not"
  14. PRINT "running this program in the integrated debugging
  15. PRINT "environment, press Ctrl-Break now.  See the debugging"
  16. PRINT "chapter in the PowerBasic User's Guide for details."
  17. PRINT
  18.  
  19. WHILE InFile$ = ""
  20.   LINE INPUT "Enter the name of the input file: ";InFile$
  21.   IF InFile$ <= STRING$(LEN(InFile$),32) THEN InFile$=""
  22.   IF InFile$ = "" THEN BEEP:PRINT "You must enter a file name!"
  23. WEND
  24.  
  25. OPEN "FYLE",1,InFile$
  26. 'If the file can't be opened, the user will get an error message.
  27.  
  28. WHILE NOT(EOF(1))            'read the file until nothing is left
  29.   LINE INPUT #1,FirstString$ 'get a line
  30.   PRINT FirstString$         'display it
  31.   WHILE FirstString$<>""
  32.     GOSUB GetAWord           'pull a word for FirstString$ and
  33.                  'put it in SecondString$
  34.     Test = LEN(b$)
  35.     IF Test <= 16 THEN
  36.       WordLength(Test) = WordLength(Test) + 1
  37.     ELSE
  38.       Overlong = Overlong + 1
  39.     END IF
  40.   WEND
  41. WEND
  42.  
  43. CLOSE 1
  44.  
  45. PRINT "Length Count"
  46. FOR Count% = 1 TO 16
  47.   PRINT Count%,WordLength(Count%)
  48. NEXT Count%
  49. PRINT "Greater";OverLong
  50. END
  51.  
  52. GetAWord:
  53.   position = INSTR(FirstString$,Blank$) 'a word is a sequence of
  54.                     'characters ended by a
  55.                     'blank or the end fo the line
  56.   IF position = 0 THEN
  57.     'the word is the remainder of the line
  58.     SecondString$ = FirstString$:FirstString$ = ""
  59.   ELSE
  60.     'pull the word from the line
  61.     SecondString$ = LEFT$(FirstString$,position-1)
  62.   END IF
  63. RETURN