| | |
QBasic character counter
Please support our Legacy and Other Languages advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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
TP
•
•
Join Date: May 2004
Posts: 40
Reputation:
Solved Threads: 0
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
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
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?
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
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 SUBThat, 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
•
•
Join Date: Mar 2006
Posts: 1
Reputation:
Solved Threads: 0
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
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
![]() |
Similar Threads
- Graphical character counter (C++)
- display the number of capitals letters in the sentence (Assembly)
- Count.Java (Java)
- Code Snippet: QBasic character counter - helped by Buff (Legacy and Other Languages)
Other Threads in the Legacy and Other Languages Forum
- Previous Thread: help me solve my matlab syntax error
- Next Thread: asp redirect help I think it is a syntax issue
| Thread Tools | Search this Thread |





