Solved

Word Count Function

Posted on 1998-03-26
9
149 Views
Last Modified: 2010-04-06
Hi,
     I need to get / write a word counting function for a TMemo object. Anyone got any code or ideas?
Cheers.
0
Comment
Question by:PiersBull
  • 6
  • 3
9 Comments
 
LVL 4

Accepted Solution

by:
d003303 earned 10 total points
ID: 1360929
var TextBuf   : PChar;
    Index,
    WordCount : Integer;
begin
  Memo1.SelectAll;
  GetMem(TextBuf, Memo1.SelLength + 2);
  try
    Memo1.GetSelTextBuf(TextBuf, Memo1.SelLength);
    TextBuf[Memo1.SelLength+ 1] := #0;
    TextBuf[Memo1.SelLength] := #32;
    Index := 0;
    WordCount := 0;
    while Index < StrLen(TextBuf) do
     begin
       if (Buffer[Index] = #32) and (Buffer[Index + 1] <> #32)
        then Inc(WordCount);
       Inc(Index);
     end;
  finally
    FreeMem(TextBuf, Memo1.SelLength + 2);
  end;
end;

Slash/d003303
0
 

Author Comment

by:PiersBull
ID: 1360930
Hi amigo,
   I've tried the code, but it gives me an unknown identifier at Buffer[index]. Any ideas?
Cheers.
0
 
LVL 4

Expert Comment

by:d003303
ID: 1360931
oops, a little typo. replace the line
if (Buffer[Index] = #32) and (Buffer[Index + 1] <> #32)
with
if (TextBuf[Index] = #32) and (TextBuf[Index + 1] <> #32)

Slash/d003303
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:PiersBull
ID: 1360932
Hi again,

    Maybe I'm being thick, but how do I output the result in a messagebox? I've tried doing an IntToStr(WordCount), but the MessageBox command gives me a type mismatch. How do I output the result?
Points upa bit.
Cheers.

0
 
LVL 4

Expert Comment

by:d003303
ID: 1360933
Application.MessageBox uses PChar strings. Declare a buffer string and typecast. e.g.
var MsgStr : string
begin
  MsgStr := Format('I have found %s words in this text', [WordCount]);
  Application.MessageBox(PChar(MsgStr), 'Note', mb_ok);
end;

Slash/d003303
0
 

Author Comment

by:PiersBull
ID: 1360934
Thanx

0
 

Author Comment

by:PiersBull
ID: 1360935
Hi amigo,
    I've tried the code, but it gives me an unknown identifier at Buffer[index]. Any ideas?
Cheers.

0
 

Author Comment

by:PiersBull
ID: 1360936
Hi amigo,
    I've tried the code, but it gives me an unknown identifier at Buffer[index]. Any ideas?
Cheers.

0
 

Author Comment

by:PiersBull
ID: 1360937
Hi amigo,
   I've tried the code, but it gives me an unknown identifier at Buffer[index]. Any ideas?
Cheers.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question