BBC BASIC for Windows
Programming >> BBC BASIC language >> Counting the number of words in a string
http://bb4w.conforums.com/index.cgi?board=language&action=display&num=1497689972

Counting the number of words in a string
Post by hinckleyj 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
Re: Counting the number of words in a string
Post by michael 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$
  

Re: Counting the number of words in a string
Post by hinckleyj 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

Re: Counting the number of words in a string
Post by michael 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$
 

Re: Counting the number of words in a string
Post by hinckleyj on Jun 17th, 2017, 7:17pm

Thank you Michael.

Spot on. This is exactly what I was looking for.

Many thanks.
John
Re: Counting the number of words in a string
Post by sveinioslo 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

Re: Counting the number of words in a string
Post by hinckleyj on Jun 22nd, 2017, 9:20pm

Thank you Svein

This also works well and allows for a double space in a sentence.

Thanks
John