Word Count

Just starting out? Need help? Post your questions and find answers here.
Summers
User
User
Posts: 15
Joined: Tue Sep 30, 2003 3:20 pm

Word Count

Post by Summers »

Is there an easy way to count words in a string? In Arexx I believe it was something like:

words("some string here")
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post by Karbon »

You could use this to count whitespace characters :

Code: Select all

Procedure.l CountChars(a.s,s.s)
  !MOV edi,dword[esp]    ;pointer to the first character in string (first function parameter)
  ;firstly we must found the lenght of the string:
  !CLD              ;clear DF (Direction Flag). Could be not necessary.
  !XOR al,al       ;set NULL character in AL register
  !XOR ebx,ebx  ;init counter
  !MOV ecx,ebx    ;lets set 4294967295 ($FFFFFFFF) characters maximum
  !DEC ecx
  !REPNZ scasb    ;repeat comparing AL CPU register content with [edi]
  !JECXZ go    ;if NULL byte is not found within those 4294967295 characters then exit giving 0
  !NOT ecx     ;else, some adjusts. Now we have the lenght at ecx register
  !MOV edi,dword[esp]     ;point again to the first character in string (first function parameter)
  !MOV eax,dword[esp+4]
  !MOV al,byte[eax]    ;al=character to find
  !@@:REPNZ scasb   ;repeat comparing AL CPU register content with [edi]
  !JECXZ go     ;until ecx value is reached
  !INC ebx       ;or a match is found
  !JMP @r       ;continue comparing next character
  !go:MOV eax,ebx   ;output the matches counter
  ProcedureReturn
EndProcedure
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
sec
Enthusiast
Enthusiast
Posts: 792
Joined: Sat Aug 09, 2003 3:13 am
Location: 90-61-92 // EU or ASIA
Contact:

Post by sec »

:)

Code: Select all

s.s="abc def a re era " ;s(len(s)) must equal = " " space
i = 0
count = 0
Repeat
 i=FindString(s," ",i+1)
 If i>0 
  count=count+1
 EndIf
Until i=0

Debug count
wayne1
User
User
Posts: 13
Joined: Sat Apr 26, 2003 4:23 am
Location: United States of America

Post by wayne1 »

To search the forums:

(1) Click the Search link at the top-right of this window.
(2) Type in your keyword(s) and click the Search button. :wink:

viewtopic.php?t=3603&highlight=countwords
sec
Enthusiast
Enthusiast
Posts: 792
Joined: Sat Aug 09, 2003 3:13 am
Location: 90-61-92 // EU or ASIA
Contact:

Post by sec »

ahh,
how use ASC() to sum of a string
sum=sum+ asc(mid(string,1,1)) <- don't do
??
Summers
User
User
Posts: 15
Joined: Tue Sep 30, 2003 3:20 pm

Post by Summers »

wayne1 wrote:To search the forums:

(1) Click the Search link at the top-right of this window.
(2) Type in your keyword(s) and click the Search button. :wink:

viewtopic.php?t=3603&highlight=countwords
No offence, but this has the worst search page ever. I've been on a lot of forums, yet whatever I do this brings a lot of results. Mostly rubbish.
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post by Karbon »

No offense, but you haven't used a lot of forum software - have you? PHPbb's searching is pretty darn good as far as forum searches go ...
Last edited by Karbon on Sun Oct 12, 2003 4:35 pm, edited 1 time in total.
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
Tomio
Enthusiast
Enthusiast
Posts: 291
Joined: Sun Apr 27, 2003 4:54 pm
Location: Germany

Post by Tomio »

sec wrote::)

Code: Select all

s.s="abc def a re era " ;s(len(s)) must equal = " " space
i = 0
count = 0
Repeat
 i=FindString(s," ",i+1)
 If i>0 
  count=count+1
 EndIf
Until i=0

Debug count
In a test I found that in larger strings (50bytes+...) FindString is much slower than a loop with: if PeekB(i) = ' ': wordcount+1:endif

Because FindString does not remember it's last call, it always has too start from the string's beginning.

../tomio
Summers
User
User
Posts: 15
Joined: Tue Sep 30, 2003 3:20 pm

Post by Summers »

Karbon wrote:No offense, but I don't think you know what you're talking about :-)
Cheap reply. Seriously, re-read my post.
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post by Karbon »

I was only joking and modified that post right after you replied!

Seriously, PHPbb's searching isn't that bad.

Click search, select "Search for all terms" type in "count words"

Bam - the "Challenge: Fastest Way To Parse And Count Words?" thread comes up. Don't know what more you could ask for :-)

It's not perfect, but the random free-form input of a forum isn't really the easiest thing to do context searching on...
Last edited by Karbon on Sun Oct 12, 2003 4:39 pm, edited 2 times in total.
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
Pupil
Enthusiast
Enthusiast
Posts: 715
Joined: Fri Apr 25, 2003 3:56 pm

Post by Pupil »

If you don't use the Option 'Search for ANY terms...' you get a better result when searching. Maybe this is of interest: viewtopic.php?t=2450&highlight=count+words
ebs
Enthusiast
Enthusiast
Posts: 561
Joined: Fri Apr 25, 2003 11:08 pm

Post by ebs »

Here's a little trick I learned for counting characters:

Code: Select all

s.s="abc def a re era "

NoSpaces.s = RemoveString(s, " ")
Debug Len(s) - Len(NoSpaces)
Eric
Fred
Administrator
Administrator
Posts: 18350
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

May be a CountString() command is needed ? :)
ebs
Enthusiast
Enthusiast
Posts: 561
Joined: Fri Apr 25, 2003 11:08 pm

Post by ebs »

Fred,

Would you consider adding something like the VB "Split" function? It fills an array with string fields separated by a specified delimiter. It's very handy for parsing words in a sentence and comma or tab delimited files.

Regards,
Eric
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post by Karbon »

A built in CountChars() would be good - that way you could count the number of any character you wanted in a string..
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
Post Reply