943,495 Members | Top Members by Rank

Ad:
Dec 19th, 2004
0

QBasic character counter

Expand Post »
Does anyone know how to count the number of characters words in a string or in anything in QBasic? I'm trying to make a hangman game and I have a list of all my words but i need to find out how to count characters for the blank spaces, you know. I would be grateful if anyone has any section of code, thanks,
TP
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Tom Pilk is offline Offline
14 posts
since Dec 2004
Dec 19th, 2004
0

Re: QBasic character counter

St$="Hello my name is John Doe"
if right$(st$)<>" " then st$=st$+" "
for k%=1 to len(st$)
if mid$(st$,k%,1)=" " then words%=words%+1
characters%=characters%+1
next
print "total characters=";characters%
print "total words=";words%

(in a sub)
declare sub getwords(st$,wds%)
xst$="Hello my name is John Doe"
GetWords xst$,words%
print "words= ";words%

SUB GetWords(st$,wds%)
if right$(st$)<>" " then st$=st$+" "
for k%=1 to len(st$)
if mid$(st$,k%,1)=" " then words%=words%+1
characters%=characters%+1
next
'print "total characters=";characters%
'print "total words=";words%
wds%=words%
end sub
Reputation Points: 11
Solved Threads: 0
Light Poster
Buff is offline Offline
40 posts
since May 2004
Dec 31st, 2004
0

Re: QBasic character counter

That doesn't seem to work. It needs a comma in:
if right$(st$)<>" " then st$=st$+" "
and when you put it in it seems to need an expression?
Any clues?!?
Thanks
TP :rolleyes:
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Tom Pilk is offline Offline
14 posts
since Dec 2004
Jan 1st, 2005
0

Re: QBasic character counter

Quote originally posted by Tom Pilk ...
That doesn't seem to work. It needs a comma in:
if right$(st$)<>" " then st$=st$+" "
and when you put it in it seems to need an expression?
Any clues?!?
Thanks
TP :rolleyes:
Sorry, should be
if right$(st$,1)....
Reputation Points: 11
Solved Threads: 0
Light Poster
Buff is offline Offline
40 posts
since May 2004
Jan 1st, 2005
0

Re: QBasic character counter

Thanks - now works perfectly. Just needed to declare the sub at the beginning rather than in the code, just a tiny error. I think it is reading one character too much though?
DECLARE SUB GetWords (st$, wds%)

st$ = "Hello my name is John Doe"
IF RIGHT$(st$, 1) <> " " THEN st$ = st$ + " "
FOR k% = 1 TO LEN(st$)
IF MID$(st$, k%, 1) = " " THEN words% = words% + 1
characters% = characters% + 1
NEXT
PRINT "total characters="; characters%
PRINT "total words="; words%

'*****Do I need anything past here? I don't think i do but it***********
'***************was in the origional guys code******************
xst$ = "Hello my name is John Doe"
GetWords xst$, words%
PRINT "words= "; words%

SUB GetWords (st$, wds%)
IF RIGHT$(st$, 1) <> " " THEN st$ = st$ + " "
FOR k% = 1 TO LEN(st$)
IF MID$(st$, k%, 1) = " " THEN words% = words% + 1
characters% = characters% + 1
NEXT
'print "total characters=";characters%
'print "total words=";words%
wds% = words%
END SUB

That, as I say, works perfectly but I don't think I need the red stuff. tell me if i am wrong though please. The thing i am actually using it for is a hangman game. Just making it for fun. All I need to know now is how to get a random word from a random file - i posted a question on it but the replies don't seem to work. Thanls anyway whoever did post replies.
TP
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Tom Pilk is offline Offline
14 posts
since Dec 2004
Mar 15th, 2006
0

Re: QBasic character counter

I just started learning Qbasic and I am having a problem trying to count words. I can count characters but the problem I'm working on requires a word count. the user must type a sentence and terminate it with a period, at which point there will be a word count echoed on the screen.

cls
LOCATE 10, 20
PRINT "Please type a sentence, terminated by a period."
LET letters$ = INPUT$(1)
LOCATE 11, 20
PRINT letters$ ;
DO UNTIL letters$ = "."
LET letters$ = INPUT$(1)
PRINT letters$ ;
count = count + 1
LOOP
LOCATE 12, 20
PRINT "word count = " ; count
END
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bs64 is offline Offline
1 posts
since Mar 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Legacy and Other Languages Forum Timeline: help me solve my matlab syntax error
Next Thread in Legacy and Other Languages Forum Timeline: asp redirect help I think it is a syntax issue





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC