'=========================================================================== ' Subject: NUMBER OF WORDS IN A STRING Date: 08-02-98 (13:08) ' Author: Wayne E. Clement Code: QB, QBasic, PDS ' Origin: clementw@prodigy.net Packet: TEXT.ABC '=========================================================================== DECLARE FUNCTION WordS% (TempLine AS STRING) DECLARE FUNCTION WordN$ (TempLine AS STRING, num AS INTEGER) REM WordS% REM Returns the number of words in a string REM WordN$ REM Returns a specific word in a string REM Both funtions treat quoted words or frases as one word REM These functions are in AREXX the Amiga version of REXX. REM I found them usefull and used then alot. When is started useing REM basic more I found i needed to write theses funtions to make string REM prossesing a little more easier. CLS DEFINT X LET TempLine$ = "one two three four five six seven eight " + CHR$(34) + "nine ten" + CHR$(34) PRINT "There are "; WordS%(TempLine$); " words in the line " PRINT " "; CHR$(39); " "; TempLine$; " "; CHR$(39) PRINT PRINT "The Words are " FOR x = 1 TO WordS%(TempLine$) PRINT x; " = "; WordN$(TempLine$, x) NEXT x REM Note the variable for the number must be defined as an integer PRINT IF COMMAND$ = "" THEN PRINT "You did not enter a command line" END IF IF COMMAND$ <> "" THEN PRINT "There are "; WordS%(COMMAND$); " words in the command line " PRINT " "; CHR$(39); " "; COMMAND$; " "; CHR$(39) PRINT PRINT "The Words are " FOR x = 1 TO WordS%(COMMAND$) PRINT x; " = "; WordN$(COMMAND$, x) NEXT x REM Note the variable for the number must be defined as an integer END IF DEFSNG X FUNCTION WordN$ (TempLine AS STRING, num AS INTEGER) DEFINT A DEFINT N DEFSTR W DEFSTR T DEFSTR Q LET Word$ = "n" LET WordFound$ = "n" LET Quote$ = "n" LET numword% = 0 FOR a% = 1 TO LEN(TempLine$) IF MID$(TempLine$, a%, 1) <> " " AND Quote$ = "n" THEN LET Word$ = "y" END IF IF MID$(TempLine$, a%, 1) = CHR$(34) THEN REM AND Quote$ = "n" THEN LET Word$ = "y" LET Quote$ = "y" END IF IF MID$(TempLine$, a%, 1) = " " AND Quote$ = "n" AND WordFound$ = "y" THEN LET Word$ = "n" LET WordFound$ = "n" END IF IF MID$(TempLine$, a, 1) = CHR$(34) AND Quote$ = "y" AND WordFound$ = "y" THEN REM Word$ = "n" REM WordFound$ = "n" LET Quote$ = "n" END IF IF WordFound$ = "n" AND Word$ = "y" THEN LET numword% = numword% + 1 LET WordFound$ = "y" END IF IF WordFound$ = "y" AND numword% = num THEN LET TempWord$ = TempWord$ + MID$(TempLine, a, 1) END IF NEXT a LET WordN$ = LTRIM$(RTRIM$(TempWord$)) END FUNCTION DEFSNG A, N, Q, T, W FUNCTION WordS% (TempLine AS STRING) DEFINT A DEFINT N DEFSTR W DEFSTR F DEFSTR Q LET Word1$ = "n" LET wordfound1$ = "n" LET Quote$ = "n" LET numword1% = 0 LET FirtsQuote$ = "n" FOR a% = 1 TO LEN(TempLine$) IF MID$(TempLine$, a%, 1) <> " " AND Quote$ = "n" THEN Word1$ = "y" IF MID$(TempLine$, a%, 1) = CHR$(34) AND Quote$ = "n" THEN LET Word1$ = "y" LET Quote$ = "y" LET FirstQuote$ = "y" END IF IF wordfound1$ = "n" AND Word1$ = "y" THEN LET numword1% = numword1% + 1 LET wordfound1$ = "y" END IF IF MID$(TempLine$, a%, 1) = " " AND Quote$ = "n" THEN LET Word1$ = "n" LET wordfound1$ = "n" END IF IF MID$(TempLine$, a%, 1) = CHR$(34) AND wordfound1$ = "y" AND FirstQuote$ = "n" THEN LET Word1$ = "n" LET wordfound1$ = "n" LET Quote$ = "n" END IF LET FirstQuote$ = "n" NEXT a% LET WordS% = numword1% END FUNCTION