Word Count
Posted: Sun Oct 12, 2003 1:37 am
Is there an easy way to count words in a string? In Arexx I believe it was something like:
words("some string here")
words("some string here")
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
EndProcedureCode: 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
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.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.![]()
viewtopic.php?t=3603&highlight=countwords
In a test I found that in larger strings (50bytes+...) FindString is much slower than a loop with: if PeekB(i) = ' ': wordcount+1:endifsec 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
Cheap reply. Seriously, re-read my post.Karbon wrote:No offense, but I don't think you know what you're talking about
Code: Select all
s.s="abc def a re era "
NoSpaces.s = RemoveString(s, " ")
Debug Len(s) - Len(NoSpaces)