Qbasic, Quickbasic/qbasic strings

Advertisement


Question
hello, I need help with my code, I have asked my teacher but she's not that nice and doesnt really make me understand what i have to do. The question to answer is:

Write a program that asks the user for a sentence and
-Prints it with one letter on each line
-Says how many words it has

I been having problems with this question dor 2 weeks now and still cant figure it out, if you could me understand that would be much appreciated

this is the code i have:
CLS

DIM sentence AS STRING
DIM length AS INTEGER
DIM spaces AS INTEGER
DIM letter AS STRING

COLOR 15
space = 0

PRINT "Enter a sentence"
INPUT sentence

FOR i = 1 TO length
letter = MID$(sentence, i, 1)
IF LEN(letter) = spaces THEN
spaces = spaces + 1
END IF
PRINT "length "; length
NEXT i

PRINT "Your sentence has"; LEN(length); "words"


Answer
Hi, Jorge,

I think I can point you in the right direction.

Your code for getting the length (your FOR loop) seems to be a little off, so I'll show you how I'd do it:

DIM words AS INTEGER

words = 0
length = LEN( sentence )
FOR i = 1 TO length
 ' Get the current letter
 letter = MID$( sentence, i, 1 )
 ' If the letter is a space, add a word ( part 2 of the question )
 IF letter = " " THEN
   words = words + 1
 END IF
 ' Print the letter on the next line ( part 1 of the question )
 PRINT letter
NEXT i
' Because the last word won't have a space after it, we add one more
words = words + 1

PRINT "The sentence has "; words; " words."

I hope the comments help,
-Alex

Qbasic, Quickbasic

All Answers


Answers by Expert:


Ask Experts

Volunteer


Alex Barry

Expertise

I have been a qbasic programmer since 2000, creating games, minor libraries and various small programs. I have experience using interrupts, graphics, file input/output, the mouse cursor, and using libraries. I have also learned FreeBASIC, QB64, c/c++, python, lua, php and html.
I do not claim to be an absolute authority in any language, but I don't mind looking things up and learning with you.

Experience

I have been programming in *Basic dialects since 2000, as mentioned in my expertise. After a year of QBasic, I learned C and C++, and dabbled a little in ASM (I don't program in ASM - I literally just played around to see how things work). When QB64 and FreeBASIC were released, I played with those languages. At the time, FreeBASIC offered more functionality and I sided with that language for a while. During that time, while I was learning new languages, that I would see what scripting languages are available, where I took up python and lua. I started to notice a staleness to QB64's development (which I kept tabs on from time to time), and am now trying to be active in it's community and maybe in it's development in the future. Currently, I am only active on the QB64.net forums, but I appear on occasion on FreeBASIC.net's forums as well.

Education/Credentials
Highschool - 2007

©2012 About.com, a part of The New York Times Company. All rights reserved.