Parsing strings

[Home]   [Puzzles & Projects]    [Delphi Techniques]   [Math topics]   [Library]   [Utilities]

 

 

Search

Search WWW

Search DelphiForFun.org

As of October, 2016, Embarcadero is offering a free release of Delphi (Delphi 10.1 Berlin Starter Edition ).     There are a few restrictions, but it is a welcome step toward making more programmers aware of the joys of Delphi.  They do say "Offer may be withdrawn at any time", so don't delay if you want to check it out.  Please use the feedback link to let me know if the link stops working.

 

Support DFF - Shop

 If you shop at Amazon anyway,  consider using this link. 

     

We receive a few cents from each purchase.  Thanks

 


Support DFF - Donate

 If you benefit from the website,  in terms of knowledge, entertainment value, or something otherwise useful, consider making a donation via PayPal  to help defray the costs.  (No PayPal account necessary to donate via credit card.)  Transaction is secure.

Mensa® Daily Puzzlers

For over 15 years Mensa Page-A-Day calendars have provided several puzzles a year for my programming pleasure.  Coding "solvers" is most fun, but many programs also allow user solving, convenient for "fill in the blanks" type.  Below are Amazon  links to the two most recent years.

Mensa® 365 Puzzlers  Calendar 2017

Mensa® 365 Puzzlers Calendar 2018

(Hint: If you can wait, current year calendars are usually on sale in January.)

Contact

Feedback:  Send an e-mail with your comments about this program (or anything else).

Search DelphiForFun.org only

 

 

 

Parsing strings into words is a common problem.  So common that I decided to post a sample GetNextWord subroutine here.  The sample program reads a text file and counts words.  I think the code is fairly self explanatory so I'll keep this discussion brief.   

The GetNextWord function takes a string and extracts  the first word. If a word is found the function result is true, otherwise false.    The returned word is removed from the input string, so it's necessary to move input  text to a temporary string first (unless you have no more use for the original text).   A constant, delims, defines the set of characters which separate words.  

The program is also an example of inserting an object  along with the word into a TStrings  component  The Items list in the TListbox component displays the words and each entry also contains a TCount object.   Each TCount  contains a single field, the count of number of occurrences of a word.   For each word we can use the Indexof method to determine if the word is already in the list.  If it is, we'll just add one to the count field in the TCount object.  If the word is not in the list,  we'll create a new TCount object, set the count field to 1 and add both the word and the object to Items.  After all words have been loaded, we can  run through the list and add the string representation of the count to the displayed word.  We'll also free the objects as we do this  since we're done with them. 

Click here to download source for Parse Strings program.     

 
  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright © 2000-2018, Gary Darby    All rights reserved.