QBasic character counter - helped by Buff

Tom Pilk 0 Tallied Votes 124 Views Share

This is a little program in QBasic which counts the number of characters in a string. Made by Buff - two errors removed by me. Thanks Buff.

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%


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
Dani 4,064 The Queen of DaniWeb Administrator Featured Poster Premium Member

Thank you ... and thank Buff. Who is Buff ;)

rhenerlau 0 Newbie Poster

The Len() function is all you really need

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.