Liberty BASIC Community Forum
« Without using the word$ function »

Welcome Guest. Please Login or Register.
Jan 23rd, 2018, 7:49pm


Rules|Home|Help|Search|Recent Posts|Notification


« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Without using the word$ function  (Read 95 times)
Chester
New Member
Image


member is offline

Avatar




PM


Posts: 6
xx Without using the word$ function
« Thread started 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
« Last Edit: Jan 11th, 2018, 9:43pm by Chester » User IP Logged

tsh73
Board Moderator

member is offline

Avatar

Anatoly (real name)


PM

Gender: Male
Posts: 1732
xx Re: Without using the word$ function
« Reply #1 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.
User IP Logged

damned Dog in the Manger
Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5618
xx Re: Without using the word$ function
« Reply #2 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.
User IP Logged

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

Rules|Home|Help|Search|Recent Posts|Notification

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