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 :confused:

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

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:

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)....

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

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

This article has been dead for over six months. Start a new discussion instead.