none
Small Basic, New User, Counting Words in a String RRS feed

  • Question

  • I am trying to write a program on smallbasic that allows me to count the number of wors in a string, but in an easy way. For example using textwindow and issubtext or getsubtext etc.

    urgent help needed before sunday the 13th March 2016

    Friday, March 11, 2016 5:43 PM

Answers

  • in a loop for i=1 to text.getLength(string)

    char= Text.getsubtext(string,i,1)

    test each char if it is a letter, dan still in a word. if another character (space, dot ...) : end of word, count and start new search.


    Jan [ WhTurner ] The Netherlands


    Friday, March 11, 2016 6:42 PM
    Answerer
  • There are lots of different ways to accomplish this problem. The most straight forward version is to simply count the number of spaces.

    str = "Counting the Words in the String"
    cnt = 1  'Assume the text starts with a word
    For i = 1 To Text.GetLength(str)
      If Text.GetSubText(str,i,1) = " " Then
        cnt = cnt + 1
      EndIf
    EndFor
    TextWindow.WriteLine(cnt)

    There are a couple of potential problems with that snipet: it assumes that words are only separated by spaces, that the string being checked starts with a word, and that blocks of letters all count as words (as apposed to digits).

    Friday, March 11, 2016 8:22 PM
    Answerer
  • if u use LD xtension, solution is so simple

    txt=" This is text to count Words. Hello world!   "

    ff=ldtext.trim(txt) ' trims xcessing spaces

    tt=ldtext.split(ff," ") ' splits into words

    ww=array.getitemcount(tt) 'counts words

    textwindow.writeline("words:"+ww)

    Saturday, March 12, 2016 3:38 AM
  • I just count the spaces for word count.
    Saturday, March 12, 2016 4:02 PM
    Moderator

All replies

  • in a loop for i=1 to text.getLength(string)

    char= Text.getsubtext(string,i,1)

    test each char if it is a letter, dan still in a word. if another character (space, dot ...) : end of word, count and start new search.


    Jan [ WhTurner ] The Netherlands


    Friday, March 11, 2016 6:42 PM
    Answerer
  • There are lots of different ways to accomplish this problem. The most straight forward version is to simply count the number of spaces.

    str = "Counting the Words in the String"
    cnt = 1  'Assume the text starts with a word
    For i = 1 To Text.GetLength(str)
      If Text.GetSubText(str,i,1) = " " Then
        cnt = cnt + 1
      EndIf
    EndFor
    TextWindow.WriteLine(cnt)

    There are a couple of potential problems with that snipet: it assumes that words are only separated by spaces, that the string being checked starts with a word, and that blocks of letters all count as words (as apposed to digits).

    Friday, March 11, 2016 8:22 PM
    Answerer
  • if u use LD xtension, solution is so simple

    txt=" This is text to count Words. Hello world!   "

    ff=ldtext.trim(txt) ' trims xcessing spaces

    tt=ldtext.split(ff," ") ' splits into words

    ww=array.getitemcount(tt) 'counts words

    textwindow.writeline("words:"+ww)

    Saturday, March 12, 2016 3:38 AM
  • I just count the spaces for word count.
    Saturday, March 12, 2016 4:02 PM
    Moderator