AllExperts > Qbasic, Quickbasic 
Search      
Qbasic, Quickbasic
Volunteer
Answers to thousands of questions
 Home · More Qbasic, Quickbasic Questions · Answer Library  · Encyclopedia ·
More Qbasic, Quickbasic Answers
Question Library

Ask a question about Qbasic, Quickbasic
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About 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, c/c++, python, 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
Hobby programming since 2000

I no longer belong to any community programming groups, but do have knowledge of *basic dialects and C/C++

 
   

You are here:  Experts > Computing/Technology > Basic > Qbasic, Quickbasic > qbasic strings

Qbasic, Quickbasic - qbasic strings


Expert: Alex Barry - 6/25/2009

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

Add to this Answer   Ask a Question


 
User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2008 About, Inc. AllExperts, AllExperts.com, and About.com are registered trademarks of About, Inc. All rights reserved.