BlitzCoder Essentials
•
Home Page
•
About BlitzCoder
•
Contributors
•
Terms of Use
•
Email Us
Main Areas
•
BlitzCoder Chat
•
Discussions
•
Articles/Tutorials
•
Code Database
•
Link Database
•
Showcase Area
•
Worklogs
•
Competitions
Special Areas
•
Undocumented
Other Blitz Sites
•
Blitz Basic Home
•
Blitz Showcase
•
BlitzPlay Library
Forum Login
Username:
Password:
•
Register Now!
BlitzCoder Code Archives Page
Main Codes Page
"Word Counter"
, by surreal
This nifty little algorithm will scan a text file for you and count the number of words in it.
Code
;===--__ ;Text file word counter program. ;Change the filename constant to whichever file you want to count. ;The algorithm works by scanning each line, byte by byte. If a letter ;is found, then the word flag is set to true. Afterwards if anything ;but a letter is found, then the flag is cleared and the ;===---/ Const Filename$ = "C:\documents and settings\john\desktop\notes\short story.doc" Graphics 300,100,16,2 File% = OpenFile (Filename$) Word% = False ;Flag to toggle between if we're looking over a word or not If File Then ;Only deal with the file if it exists While Not Eof(File) ;Read until file is finished stLine$ = ReadLine$ (File) For scan = 1 To Len(stLine$) char% = Asc (Mid$(stLine$,scan,1)) If ((char > 64) And (char < 91)) Or ((char > 96) And (char < 123)) Then letter% = True Else letter = False If word Then ;If we're examining a word If (Not(letter)) Or (scan = Len(stLine)) Then word = False WordCount = WordCount + 1 End If Else If letter Then word = True End If End If Next If KeyHit(1) Then Exit Wend CloseFile File Else RuntimeError "File could not be opened" End If AppTitle WordCount + " words found." st$ = "Words counted: " + WordCount Text 300/2,100/2,st,True,True WaitKey
Copyright(c) 2000-2004, BlitzCoder. All Rights Reserved.
Code software created by Krylar's Kreations