Qbasicnews.com

Full Version: need quick response
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
how do i get a program that identifies the amount of words in a sentence.



example-



Hello little one



and it would print 3
Topic is rather insulting.

Count the spaces.
OK...
Code:
yourstr$="your stuff here"
spaceCount=1
FOR i=1 to LEN(yourstr$)
IF MID$(yourstr$,i,1)=" " THEN spaceCount=spaceCount+1
NEXT
PRINT spaceCount
^Untested
EDIT: Works.
zack, you might as well have just emailed that to his teacher.
Sorry, sorry...
Seraph, think it over and make sure you understand it all before you hand it in. :roll:
It doesn't work completely anyway... what about sentences with double spaces? I had to make a proggie like this for comp sci, and it's a bit trickier than it looks...
Ah, well, I was assuming things are single-spaced...I can integrate it to ignore a space right after another.
OK, here, it deals with double-spacing:
Code:
yourstr$ = "your  stuff  here.  and cool  yay"
spaceCount = 1
FOR i = 1 TO LEN(yourstr$)
IF MID$(yourstr$, i, 1) = " " THEN
spaceCount = spaceCount + 1
IF MID$(yourstr$, i + 1, 1) = " " THEN i = i + 1
END IF
NEXT
PRINT spaceCount
Code:
MyStr$ = "your  stuff  here.  and cool  yay"

spaced% = 0
spaces% = 1
FOR I% = 1 TO LEN(MyStr$)
   z$ = MID$(MyStr$, I%, 1)
   IF z$ = CHR$(32) AND NOT(spaced%) THEN
      spaces% = spaces% + 1
      spaced% = -1
   ELSEIF MID$(MyStr$, I%, 1) <> CHR$(32) THEN
      spaced% = 0
   END IF
NEXT I%
IF RIGHT$(MyStr$, 1) <> CHR$(32) THEN spaces% = spaces% - 1
PRINT "Number of words ="; spaces%

I think this flag-theory is better instead of front-scan theory Wink
Why?
That way works too, but...is it faster? I don't really see improvement. Smile

Anonymous

its because his code is 'dynamic' that is, it can handle *any* number of spaces. yours can only handle double spacing. IMO, its always better to write code that is universal...
Pages: 1 2 3