BBC BASIC for Windows
« Counting the number of words in a string »

Welcome Guest. Please Login or Register.
Apr 5th, 2018, 9:53pm



ATTENTION MEMBERS: Conforums will be closing it doors and discontinuing its service on April 15, 2018.
Ad-Free has been deactivated. Outstanding Ad-Free credits will be reimbursed to respective payment methods.

If you require a dump of the post on your message board, please come to the support board and request it.


Thank you Conforums members.

BBC BASIC for Windows Resources
Online BBC BASIC for Windows documentation
BBC BASIC for Windows Beginners' Tutorial
BBC BASIC Home Page
BBC BASIC on Rosetta Code
BBC BASIC discussion group
BBC BASIC for Windows Programmers' Reference

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Counting the number of words in a string  (Read 665 times)
hinckleyj
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 14
xx 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
User IP Logged

michael
Senior Member
ImageImageImageImage


member is offline

Avatar




PM


Posts: 335
xx 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 » User IP Logged

I like making program generators and like reinventing the wheel
hinckleyj
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 14
xx 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
User IP Logged

michael
Senior Member
ImageImageImageImage


member is offline

Avatar




PM


Posts: 335
xx 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 » User IP Logged

I like making program generators and like reinventing the wheel
hinckleyj
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 14
xx 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
User IP Logged

sveinioslo
Developer

member is offline

Avatar




PM


Posts: 64
xx 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
User IP Logged

hinckleyj
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 14
xx 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
User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

| |

This forum powered for FREE by Conforums ©
Terms of Service | Privacy Policy | Conforums Support | Parental Controls