Solved

Word Count Function

Posted on 1998-03-26
9
144 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now