Solved

Counting number of words

Posted on 1998-11-09
10
281 Views
Last Modified: 2010-04-04
Does anyone know an easy way to count the number of words in a string (memo.text) ?
0
Comment
Question by:webers
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 5

Expert Comment

by:ronit051397
ID: 1346291
This example searches for strings that are not numbers and have at list 2 characters.
Your definition of "words" can be different and you probably improve this code, because it's not perfect and maybe has some bugs, but just to show you the main idea.

procedure TForm1.Button1Click(Sender: TObject);
var Buf, Point1: pChar;
    I: Word;
begin
  Buf := Memo1.Lines.GetText;
  I:=0;
  Point1:=Buf;
{first we check the spaces}
  while Point1<>nil do
  begin
    if Point1<>nil then
    if not ((Point1[0] in [' ', #13, #10, #0, '0'..'9']) or
      (Point1[1] in [' ', #13, #10, #0, '0'..'9'])) then Inc(I);
    Point1:=StrScan(Point1, ' ');
    if Point1<>nil then
      while Point1[0]=' ' do Inc(Point1);
  end;
{then we check the linefeeds}
  Point1:=Buf;
  Point1:=StrScan(Point1, #10);
  while Point1<>nil do
  begin
    if not ((Point1[1] in [' ', #13, #10, #0,'0'..'9']) or
    (Point1[2] in [' ', #13, #10, #0,'0'..'9'])) then Inc(I);
    Inc(Point1);
    Point1:=StrScan(Point1, #10);
  end;
  StrDispose(Buf);
  Showmessage('number of words is: '+inttostr(i));
end;

0
 
LVL 10

Expert Comment

by:viktornet
ID: 1346292
Hello y'all.. I just took a peek at the code, and wanted to to comments something... You don't need to StrDispose(Buf) since you never really took memory for that...

Cheers,
Viktor
0
 

Author Comment

by:webers
ID: 1346293
Hi !

This code nearly did it ! The prob is the improving.
I used ['A'..'Z', 'a'..'z'] instead of [' ', #13, #10, #0, '0'..'9'].
But still some bizarre chars are recognized as words.
I'm not that in that kind og programming.

Any idea ?
--heiko
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

 
LVL 10

Expert Comment

by:viktornet
ID: 1346294
You can't use ['A'..'Z', 'a'..'z'] instead of [' ', #13, #10, #0, '0'..'9'] because it says NOT before you use [' ', #13, #10, #0, '0'..'9'] which means that this is exacty what you need....try it without changing anything, and then say what you need...
0
 
LVL 5

Expert Comment

by:ronit051397
ID: 1346295
Vikronet, at the end we have to call StrDispose. From Borland's help file:
"...Call GetText to obtain a dynamically allocated character buffer containing all of the strings in the list. Individual strings are separated by a carriage return and line feed. The caller is responsible for freeing the returned value..."<---

Webers, I have showed you the main idea, assuming you know how to continue from that point.
Ronit
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1346296
Yeah I just read it... You are right ... 10x :)

Cheers,
Viktor
0
 
LVL 2

Expert Comment

by:333
ID: 1346297
Hi,
there is another way to do this.
You can create a TStringList and then assign Memo.Lines to TStringList.CommaText:

procedure TForm1.Button1Click(Sender: TObject);
var
  sl:TStringList;
  a,count:longint;
begin
  sl:=TStringList.Create;
  count:=0;
  for a:=0 to Memo1.Lines.Count-1 do begin
   sl.Clear;
   sl.CommaText:=Memo1.Lines[a];
   count:=count+sl.Count;
  end;
end;

So, count will be the number of words.
This code recognizes words separated by ' , '   ' ; '  spaces and #10#13 (CR), but if your text is within " " (double quotes), this text will be count as one word.

A.
0
 
LVL 5

Expert Comment

by:ronit051397
ID: 1346298
333, I didn't check your code, but in general, working with pointers should be faster.
0
 

Author Comment

by:webers
ID: 1346299
Hi !

333, how about this ?
Changes I made aren't the best, but it works.
I fixed the "" problem and now only
"real" words are counted (excluding ?, * and so on).

Hello, y'all up there, especially ronit, thanks
for your answers.
(ronit, I forgot to tell you that I removed the "not"s.)
333, post your comment as answer that I can give
you the points.

take care,
--heiko.


var
  sl: TStringList;
  a, i, count: longint;
  st, ct: string;
  p: Pchar;
begin
  sl := TStringList.Create;
  Count := 0;

  For a := 0 to GetCurMemo.Lines.Count - 1 do begin
   sl.Clear;  //getcurmemo is an Trichedit
   ct:= GetCurmemo.Lines[a];

   while Pos('"', ct) > 0 do   // the "" problem
    Delete(ct, Pos('"', ct), 1);
   sl.CommaText := ct;

    For I := 0 to sl.Count - 1 do begin
      try
       st := sl[I];
       p := Pchar(st);
       if not (p[0] in ['A'..'Z', 'a'..'z', 'Ä', 'ä', 'Ö', 'ö', 'Ü', 'ü', 'ß']) then sl.Delete(I);
      except end; //these signs after 'a'..'z' are german umlauts
    End;
    count := count + sl.Count;
  End;
  ShowMessage(IntToStr(Count));
0
 
LVL 2

Accepted Solution

by:
333 earned 50 total points
ID: 1346300
Hi,
       there is another way to do this.
       You can create a TStringList and then assign Memo.Lines to TStringList.CommaText:

       procedure TForm1.Button1Click(Sender: TObject);
       var
         sl:TStringList;
         a,count:longint;
       begin
         sl:=TStringList.Create;
         count:=0;
         for a:=0 to Memo1.Lines.Count-1 do begin
          sl.Clear;
          sl.CommaText:=Memo1.Lines[a];
          count:=count+sl.Count;
         end;
       end;

       So, count will be the number of words.
       This code recognizes words separated by ' , '   ' ; '  spaces and #10#13 (CR), but if your text is within " " (double quotes), this
       text will be count as one word.

       A.
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

Suggested Solutions

Title # Comments Views Activity
Intraweb download file link ? 1 156
URL for downloading Google Chrome for Win XP 2 191
Delphi: sending SMS on android platform 1 38
DBCtrlGrid, Delphi, Scroll 8 38
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

828 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