karbon: Nb = CountString(String$, StringToCount$). You could of course provide a single char string if needed
Word Count
Eeeeeeeeeexcellent!
-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
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
-
FloHimself
- Enthusiast

- Posts: 229
- Joined: Wed May 14, 2003 3:38 pm
- Location: Lüneburg - Germany
hi,
until fred implemented this natively to purebasic,
you can use the "Tally" function from this userlib.
its doing the same as the mentioned
CountString(String$, StringToCount$)
see example..
http://www.florian-s.com/download/PureBasic/PBTally.zip
until fred implemented this natively to purebasic,
you can use the "Tally" function from this userlib.
its doing the same as the mentioned
CountString(String$, StringToCount$)
see example..
http://www.florian-s.com/download/PureBasic/PBTally.zip
Last edited by FloHimself on Mon Oct 13, 2003 9:40 pm, edited 1 time in total.
-
Froggerprogger
- Enthusiast

- Posts: 423
- Joined: Fri Apr 25, 2003 5:22 pm
- Contact:
Errr... What about a string like
There are two words inside it, but your Procedures returns 19 
Code: Select all
"word1 word2"%1>>1+1*1/1-1!1|1&1<<$1=1
To work properly any word count function would have to look at whitespace in context..
PHP's str_word_count() says :
PHP's str_word_count() says :
Which sounds about right to me..For the purpose of this function, 'word' is defined as a locale dependent string containing alphabetic characters, which also may contain, but not start with "'" and "-" characters.
-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
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
-
FloHimself
- Enthusiast

- Posts: 229
- Joined: Wed May 14, 2003 3:38 pm
- Location: Lüneburg - Germany
thats correct. if you set StringToCount$ = " ", it counts all spaces.
in your example 19
this function will not count "words" of a string! it counts how often the string/char occurs in a string.
btw:
i have wrapped some fuctions from a regular expressions library to a purebasic user library. it works well and maybe its more useful to count real "words". at the moment i wait for a statement from the author, if i am allowed to use / release here such a wrapper.
[edit]
in the moment i was typing this post, i recieved the awaited mail.
in your example 19
this function will not count "words" of a string! it counts how often the string/char occurs in a string.
btw:
i have wrapped some fuctions from a regular expressions library to a purebasic user library. it works well and maybe its more useful to count real "words". at the moment i wait for a statement from the author, if i am allowed to use / release here such a wrapper.
[edit]
in the moment i was typing this post, i recieved the awaited mail.
so wait until i've done some documentation, then i will release the regexp lib...Yes, go ahead, no problems.
-
FloHimself
- Enthusiast

- Posts: 229
- Joined: Wed May 14, 2003 3:38 pm
- Location: Lüneburg - Germany
here is a "CountWords" user lib.. a fast hack!
see example..
http://www.florian-s.com/download/PureB ... tWords.zip
see example..
http://www.florian-s.com/download/PureB ... tWords.zip
-
FloHimself
- Enthusiast

- Posts: 229
- Joined: Wed May 14, 2003 3:38 pm
- Location: Lüneburg - Germany
and here the mentioned regexp purebasic user lib.
http://www.florian-s.com/download/PureB ... RegExp.zip
- Psychophanta
- Always Here

- Posts: 5153
- Joined: Wed Jun 11, 2003 9:33 pm
- Location: Anare
- Contact:
Summers, i made this useful procedure in ASM some months ago (i didn't make a PBlib with it because i thought it would be included soon in standard PB):
and you can find this and another good things at the Code-Archive in www.purearea.net, in String-Handling section, thanx to Andre Beer.
Code: Select all
Procedure.l CountStrings(a.s,s.s)
!cld ;clear DF (Direction Flag)
!mov edi,dword[esp] ;load edi register with pointer to first string
!mov esi,dword[esp+4] ;load esi register with pointer to second string (the string to search)
!xor eax,eax ;set eax register to NULL
!mov bl,al ;set bl register to NULL
!mov edx,esi ;save this value in edx to avoid, as much as possible, to have to read data from memory in the main loop.
!;If any of two strings is empty then end program and matches found is 0;
!cmp byte[edi],bl ;test if first string is empty
!jz fin ;if so, then end
!cmp byte[esi],bl ;test if second string is empty
!jz fin ;if so, then end
!;Mainloop:
!mainloop:
!cmpsb ;what this instruction do is just compare byte[edi] with byte[esi] and increment edi and esi values by 1
!jz match ;if byte[edi]=byte[esi] then goto match label, because a match byte was found...
!mov esi,edx ;restore this
!cmp byte[edi],bl ;check if end of first string is reached
!jnz mainloop ;if not reached then compare next bytes
!fin:
ProcedureReturn
!match: ;here we have got inside a second treatment: We are in a possible total match, lets see if it is a complete match or it is a fake:
!cmp byte[esi],bl ;check if end of second string is reached
!jz @f ;if so, here was a complete match
!cmpsb ;compare one more byte
!jz match ;if equal, lets see if the deceit continues, or rather it could be a real complete match.
!dec edi ;ohhh! it was a deceit! It wasn't a complete match. So return back the first string counter by 1.
!mov esi,edx ;and restore this
!jmp mainloop ;What a patient! lets continue searching for another possible match and why not, a possible complete match...
!;complete match was found:
!@@:inc eax ;increment complete matches counter
!mov esi,edx ;restore this
!jmp mainloop ;lets search for another possible complete match!
EndProcedure
;Grrr!: Z-80 had conditional CALL, Ix86 doesn't !?
a$="ureruPururePe ruPurePurPuPure uresdfhg ureeurPeruPuruee sPuuredil hPur eurePuree upurerePuret"
MessageRequester("",Str(CountStrings(a$,"Pure")),0)
;Can use function input parameters as normal pointers:
;Procedure.l CountStrings(*a,*s)
;but then you have to call function using pointers too:
;CountStrings(@a$,@"Pure")This one seemed to work for me, where there was no duplicated whitespace:

Code: Select all
Procedure CountWords(String.s)
Repeat
CurrentByte = @String + i
If PeekB(CurrentByte) = 32
Count + 1
i + 1
Continue
EndIf
i + 1
Until PeekB(CurrentByte) = 0
ProcedureReturn Count + 1
EndProcedure
- Psychophanta
- Always Here

- Posts: 5153
- Joined: Wed Jun 11, 2003 9:33 pm
- Location: Anare
- Contact:
-
PB&J Lover
- Enthusiast

- Posts: 212
- Joined: Fri Apr 22, 2005 2:07 pm
- Location: U.S.A.
- Contact:
Word Count
Here are two simple procedures that work together to do it.
Code: Select all
Procedure.s RemoveExtraSpaces(Text$)
While FindString(Text$," ",1) : Text$ = ReplaceString(Text$," "," ", 1) : Wend
ProcedureReturn Text$
EndProcedure
Procedure.l WordCount(Text$)
count.l = 1 : Text$ = RemoveExtraSpaces(Text$)
While StringField(Text$, count, " ") : count + 1 : Wend
ProcedureReturn count - 1
EndProcedure-- DB
Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius — and a lot of courage — to move in the opposite direction.
Albert Einstein
Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius — and a lot of courage — to move in the opposite direction.
Albert Einstein


