• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 154
  • Last Modified:

Word Count Function

Hi,
     I need to get / write a word counting function for a TMemo object. Anyone got any code or ideas?
Cheers.
0
PiersBull
Asked:
PiersBull
  • 6
  • 3
1 Solution
 
d003303Commented:
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
 
PiersBullAuthor Commented:
Hi amigo,
   I've tried the code, but it gives me an unknown identifier at Buffer[index]. Any ideas?
Cheers.
0
 
d003303Commented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
PiersBullAuthor Commented:
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
 
d003303Commented:
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
 
PiersBullAuthor Commented:
Thanx

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

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

0
 
PiersBullAuthor Commented:
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now