Author |
Topic: Counting the number of words in a string (Read 666 times) |
|
hinckleyj
New Member
member is offline


Gender: 
Posts: 14
|
 |
Counting the number of words in a string
« Thread started on: Jun 17th, 2017, 08:59am » |
|
Hi All,
Is there a way to reliably count the number of words in a string?
I know LEN counts the characters, but I need to find the number of words excluding spaces?
Many thanks
John
|
|
Logged
|
|
|
|
michael
Senior Member
member is offline


Posts: 335
|
 |
Re: Counting the number of words in a string
« Reply #1 on: Jun 17th, 2017, 1:34pm » |
|
Try this out This program extracts each word.
You can modify it to count each word too
Check in the Tools section in this forum for more ideas.
Code: g$="happy halloween my fellow programmers "
REPEAT
h$=FNnxtwrd(g$)
IF h$<>"" THEN PRINT h$
UNTIL h$=""
END
DEFFNnxtwrd(c$)
PRIVATE cnt%
LOCAL ret$
cnt%=cnt%+1
WHILE MID$(c$,cnt%,1)<>" "
ret$=ret$+MID$(c$,cnt%,1)
cnt%=cnt%+1
ENDWHILE
IF ret$="" THEN cnt%=0
=ret$
|
« Last Edit: Jun 17th, 2017, 1:46pm by michael » |
Logged
|
I like making program generators and like reinventing the wheel
|
|
|
hinckleyj
New Member
member is offline


Gender: 
Posts: 14
|
 |
Re: Counting the number of words in a string
« Reply #2 on: Jun 17th, 2017, 3:34pm » |
|
Thanks Michael. That works a treat, but I cannot see how to actually count the number of words, which was what I was after.
I've looked around the forum but failed to find the answer.
Thanks
|
|
Logged
|
|
|
|
michael
Senior Member
member is offline


Posts: 335
|
 |
Re: Counting the number of words in a string
« Reply #3 on: Jun 17th, 2017, 5:38pm » |
|
Here is the word counter version you wanted: To narrow down the possibility of mistaken words a person could add more conditions like:
IF h$<>"," AND h$<>"." AND h$<>"?" AND h$<>"!" AND h$<>"" THEN PRINT h$:amnt%+=1
Code: g$="happy halloween my fellow programmers "
amnt%=0
REPEAT
h$=FNnxtwrd(g$)
REM you could replace the following line with the sample line I shown in the description
IF h$<>"" THEN PRINT h$:amnt%+=1
UNTIL h$=""
PRINT "There are "+STR$(amnt%)+" words in the sentence."
END
DEFFNnxtwrd(c$)
PRIVATE cnt%
LOCAL ret$
cnt%=cnt%+1
WHILE MID$(c$,cnt%,1)<>" "
ret$=ret$+MID$(c$,cnt%,1)
cnt%=cnt%+1
ENDWHILE
IF ret$="" THEN cnt%=0
=ret$
|
« Last Edit: Jun 17th, 2017, 5:45pm by michael » |
Logged
|
I like making program generators and like reinventing the wheel
|
|
|
hinckleyj
New Member
member is offline


Gender: 
Posts: 14
|
 |
Re: Counting the number of words in a string
« Reply #4 on: Jun 17th, 2017, 7:17pm » |
|
Thank you Michael.
Spot on. This is exactly what I was looking for.
Many thanks. John
|
|
Logged
|
|
|
|
sveinioslo
Developer
member is offline


Posts: 64
|
 |
Re: Counting the number of words in a string
« Reply #5 on: Jun 20th, 2017, 7:37pm » |
|
Alternative code using the stringlibrary.
Code: g$="happy halloween my fellow programmers "
INSTALL @lib$+"stringlib"
g$=FN_trim(g$) : REM remove leading and trailing spaces, (if any)
WHILE FN_findreplace(g$," "," ",0) : ENDWHILE : REM make sure there is only one space between words, (if needed)
PRINT FN_split(g$," ",a$()) : REM count words
Svein
|
|
Logged
|
|
|
|
hinckleyj
New Member
member is offline


Gender: 
Posts: 14
|
 |
Re: Counting the number of words in a string
« Reply #6 on: Jun 22nd, 2017, 9:20pm » |
|
Thank you Svein
This also works well and allows for a double space in a sentence.
Thanks John
|
|
Logged
|
|
|
|
|