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

Welcome Guest. Please Login or Register.
Jan 20th, 2018, 4:22pm


Cross-platform BBC BASIC (Win32, Linux x86, Android, Mac OS-X, Raspberry Pi)

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


member is offline

Avatar




PM


Posts: 3
xx Counting the number of words in a string
« Thread started on: Jun 17th, 2017, 09:17am »

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

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 689
xx Re: Counting the number of words in a string
« Reply #1 on: Jun 17th, 2017, 10:21am »

on Jun 17th, 2017, 09:17am, Hinckleyj wrote:
Is there a way to reliably count the number of words in a string?

The key point is your use of the word "reliably". If you are happy to define a 'word' as any string of characters delimited by one or more spaces then this will work:

Code:
      PRINT FNwordcount("The quick  brown fox jumps   over the lazy dog.")
      END

      DEF FNwordcount(a$)
      LOCAL C%,I%
      IF a$ <> "" C% = 1
      REPEAT
        I% = INSTR(a$," ",I%)
        IF I% THEN
          C% += 1
          REPEAT I% += 1 : UNTIL MID$(a$,I%,1) <> " "
        ENDIF
      UNTIL I% = 0
      = C% 

But it may not give the 'correct' answer if there are line breaks and/or paragraph breaks in the string, nor if there are leading or trailing spaces. You could perhaps work around this by pre-processing the string to convert line breaks into spaces, and removing leading and trailing spaces, but then hyphenated words crossing a line-break may be mis-counted. And then there are issues such as the presence of 'soft spaces' and other special delimiters.

It all depends on just how "reliable" you need it to be! The relevant Wikipedia article shows that the definition of 'word' is far from universal or straightforward.

Richard.
User IP Logged

hellomike
Junior Member
ImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 55
xx Re: Counting the number of words in a string
« Reply #2 on: Jun 17th, 2017, 10:24am »

John,

If you are confident that words are separated by spaces ONLY, then check out the FN_split() function from the STRINGLIB library.

However if you also need to consider commas and semicolons and the like, then you need to write your own code for that. In a loop, you would need to check every character in the string and increment the word count as soon as a character is a non-letter followed by a letter.

Hope this helps.

Mike


Oops Richard answered before I finished this.......
« Last Edit: Jun 17th, 2017, 10:26am by hellomike » User IP Logged

Hinckleyj
New Member
Image


member is offline

Avatar




PM


Posts: 3
xx Re: Counting the number of words in a string
« Reply #3 on: Jun 17th, 2017, 10:29am »

Thanks to Richard and Hellomike.

Richard, your code will actually work for my purpose, thank you, but I am looking at the Stringlib Library to see what that can offer...

Thanks guys.

John
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 689
xx Re: Counting the number of words in a string
« Reply #4 on: Jun 17th, 2017, 10:39am »

on Jun 17th, 2017, 10:29am, Hinckleyj wrote:
I am looking at the Stringlib Library to see what that can offer...

There's nothing relevant in STRINGLIB, sorry. Whilst FN_split() will return a 'word' count it's hopelessly inefficient (not least in the use of memory) if that's all you need. It's also confused by something as simple as words separated by two or more spaces (I'm old and traditional enough still always to put two spaces after every full-stop).

Richard.
User IP Logged

Hinckleyj
New Member
Image


member is offline

Avatar




PM


Posts: 3
xx Re: Counting the number of words in a string
« Reply #5 on: Jun 17th, 2017, 10:43am »

Quote:
There's nothing relevant in STRINGLIB, sorry. Whilst FN_split() will return a 'word' count it's hopelessly inefficient (not least in the use of memory) if that's all you need. It's also confused by something as simple as words separated by two or more spaces (I'm old and traditional enough still always to put two spaces after every full-stop).


Thanks Richard. I also put 2 spaces after a full stop (we must be about the same age smiley)

I'll use your code at the top of this post.

Thanks again.

John
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 689
xx Re: Counting the number of words in a string
« Reply #6 on: Jun 17th, 2017, 11:25am »

on Jun 17th, 2017, 10:43am, Hinckleyj wrote:
I also put 2 spaces after a full stop (we must be about the same age smiley)

Style guides say you should only do it when using a monospaced font (it arose in the days of typewriters) not when proportional-spaced, as is usual these days. But the habit is too ingrained for me to stop now; some editors will remove the second space anyway.

Richard.
User IP Logged

jgharston
New Member
Image


member is offline

Avatar




PM


Posts: 12
xx Re: Counting the number of words in a string
« Reply #7 on: Oct 13th, 2017, 11:56pm »

on Jun 17th, 2017, 11:25am, Richard Russell wrote:
Style guides say you should only do it when using a monospaced font (it arose in the days of typewriters) not when proportional-spaced, as is usual these days.

Interesting, I was taught the other way around, one space for monospace font because you've got less "visual space" avaialble, two for proportional as the characters squash down to give more space.
User IP Logged

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

Donate $6.99 for 50,000 Ad-Free Pageviews!


This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls