Liberty BASIC Community Forum
Liberty BASIC Programming Discussions >> Novice >> Without using the word$ function
http://libertybasic.conforums.com/index.cgi?board=novice&action=display&num=1515720226

Without using the word$ function
Post by Chester on Jan 11th, 2018, 7:23pm

a$ = "chester jeanette malachi michelle"
for b = 1 to len(a$)'Count the letters in a$ string
if b = 5 then exit for
print b
next b
end

This is a Exercise Quiz in Carl Gundel book (Beginning Programming With Liberty Basic:
--------------------------------------------------------------
Do anybody know a way to print the (4) names instill of the numbers without using the word$ function? he want me to use the for/next loop and not the word$ function to do this.
this little program will print the (4) numbers. is it possible to do this without using word$? I been playing around a long time with this. I am trying to do all the examples in his book.

chester
Jeanette
malachi
michelle
Re: Without using the word$ function
Post by tsh73 on Jan 12th, 2018, 01:08am

1) Quote:
he want me to use the for/next loop and not the word$ function to do this

Carl Gundel wants you to do something? Really? He reads this forum - I think he will be surprised to hear that ;)

2) Quote:
and not the word$ function

as was said several times, function WORD$ is easiest(often best) way to work with words (surprise!)
But if you persist, there are other ways, sure

3) Ok, on FOR loops, words and not using WORD$
3a) 4 names
- you take letter one-by-one (with mid$)
- you print that letter in same line (PRINT ends with (;))
- you check if word is over (that is, you hit a space), you wrap line (empty PRINT)
that's all.
Checking:
Code:
a$ = "chester jeanette malachi michelle"
for i = 1 to len(a$)    'all the letters in a$ string
    c$=mid$(a$,i,1) 'i-th letter
    print c$;   'semicolon makes it print on same line
    if c$=" " then print 'wrap line if word is over
next 'you don't need to write variable here
end 


3b) word numbers.
Quote:
this little program will print the (4) numbers. is it possible to do this without using word$
Well, your program prints "(4) numbers" ALWAYS. If you make a$ contain 6 words it still will print 4.
Suppose you want output a number for each word. So:
- you make new variable for wordNumber, starting from 0
- you take letter one-by-one (with mid$)
- you check if word is over (that is, you hit a space),
-- you increase wordNumber and print it.
That would be all but it leaves last word (no space after it)... so after NEXT we once again
-- (increase wordNumber and print it.)
That's all.
Checking:
Code:
a$ = "chester jeanette malachi michelle"
wordNumber=0
for i = 1 to len(a$)    'all the letters in a$ string
    c$=mid$(a$,i,1) 'i-th letter
    if c$=" " then
        wordNumber=wordNumber+1
        print wordNumber
    end if
next
'don't forget the last word
wordNumber=wordNumber+1
print wordNumber
end 


Now, these programs will fail if words are separated with multiple spaces. Solution with WORD$ would work just fine.
Re: Without using the word$ function
Post by Rod on Jan 12th, 2018, 05:45am

It is an exercise Carl wrote, page 59 of Beginning Programming with Liberty BASIC.

So hunting for the space character is the way to do it. You could add a space to the end of the sentence to make it easier to get all the words out.