BBC BASIC « Counting the number of words in a string » Welcome Guest. Please Login or Register.Mar 31st, 2018, 10:43pm

ATTENTION MEMBERS: Conforums will be closing it doors and discontinuing its service on April 15, 2018.
We apologize Conforums does not have any export functions to migrate data.
Ad-Free has been deactivated. Outstanding Ad-Free credits will be reimbursed to respective payment methods.

Thank you Conforums members.
 Cross-platform BBC BASIC (Windows, Linux x86, Mac OS-X, Android, iOS, Raspberry Pi) BBC BASIC Resources BBC BASIC Help Documentation BBC BASIC for Windows Home Page BBC BASIC Programmers' Reference BBC BASIC Beginners' Tutorial BBC BASIC for SDL 2.0 Home Page BBC BASIC Discussion Group

 BBC BASIC Programming BBC BASIC Language (Moderator: Richard Russell)  Counting the number of words in a string « Previous Topic | Next Topic »
 Pages: 1
 Author Topic: Counting the number of words in a string  (Read 602 times)
Hinckleyj
New Member

member is offline

Posts: 3
 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

 Logged

Richard Russell

member is offline

Posts: 803
 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.
 Logged

hellomike
Junior Member

member is offline

Gender:
Posts: 55
 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 » Logged

Hinckleyj
New Member

member is offline

Posts: 3
 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
 Logged

Richard Russell

member is offline

Posts: 803
 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.
 Logged

Hinckleyj
New Member

member is offline

Posts: 3
 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 )

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

Thanks again.

John
 Logged

Richard Russell

member is offline

Posts: 803
 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 )

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.
 Logged

jgharston
New Member

member is offline

Posts: 12
 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.
 Logged

 Pages: 1
 Forum Jump ------------------ => General ------------------ - General Board - Announcements - Support and Promote ------------------ => Development ------------------ - Interpreter & Run-Time Engine - Integrated Development Environment - Linux (86) - Mac OS-X - Android - Raspberry Pi - iOS ------------------ => Programming ------------------ - BBC BASIC Language - Operating System Interface - VDU Emulation - Libraries - Assembly Language - Communication and Input/Output - Database and Files - Graphics and Games - Sound, Music and Video - User Interface « Previous Topic | Next Topic »

| |