The QBasic Forum      Other Subforums, Links and Downloads
 Return to Index  

Simple Word Search Routine...

May 15 2006 at 4:32 PM
Pete  (no login)

CLS
WORD$ = "Patty cake, patty cake, baker's man. Bake me a cake as fast as you can."
REM I know, Pattycake is all one word but cut me some slack for this example.
PRINT WORD$: PRINT
LINE INPUT "Search word ", SRCH$
SRCH$ = LTRIM$(RTRIM$(SRCH$))
DO
IF INSTR(UCASE$(MID$(WORD$, MARKER% + 1)), UCASE$(SRCH$)) <> 0 THEN
MARKER% = INSTR(MARKER% + 1, UCASE$(WORD$), UCASE$(SRCH$))
IF MARKER% + LEN(SRCH$) > LEN(WORD$) OR INSTR(";,.!?: ", MID$(WORD$, MARKER% + LEN(SRCH$), 1)) <> 0 THEN
COLOR 7, 1
LOCATE 1, MARKER%
PRINT MID$(WORD$, MARKER%, LEN(SRCH$));
MARKER% = MARKER% + LEN(SRCH$)
END IF
ELSE
EXIT DO
END IF
LOOP

---------------------------------------------------

The routine as designed finds words, not letters. Try a search for 'cake' and see what happens. Try again with 'cak' and no results will be returned. It will compensate for commonly used puntuation marks.

Anyone new to QB or new to string manipulation may find the following discussion helpful:

UCASE$() is used to allow differences in case to be ignored. For instance, if you type in 'patty' for the search word, the string variable SRCH$ = "patty" will be changed by UCASE$() to 'PATTY' in the program. Since UCASE$() is also used to convert every letter in the sentence to a cap, SRCH$ looks for every occurrence of 'PATTY' in the now all-caps sentence and finds two of them. If UCASE$() was not used, 'Patty' would get ignored because the capital "P" would not match the search criteria for 'patty' that was input into the program.

INSTR() is used to find where in a string a declared element is located. For example, if you code:

A$ = "abcde"
X = INSTR(A$, "c")

X would equal 3 because 'c' is the third-character in the string. However, INSTR() has another parameter you can add, which starts INSTR() counting from a designated place within that string. For example:

A$ = "abcdeabcde"
X = INSTR(3 + 1, A$, "c")

In the above example, three was entered since the first search for 'c' was 3. A '1' was added to pass it by, so it would not be looked at again. Now, the statement returns a value of 8, which is where the letter 'c' appears the second time. In the program, the variable 'MARKER%' is used to track this first element of the INSTR() statement.

MID$() is a useful way of finding a part of a string. It has three parameters, the string name, the starting point in the string, and the number of characters to in include. If this third parameter is left out, MID$() just includes the string from the starting point to the last character. For example:

A$ = "abcdeabcde"
PARTA$ = MID$(A$, 4, 3)

In this example, PARTA$ would be: 'dea' as 'd' is the starting point, 4th character, and 'dea' is three characters long. If the third parameter was left out...

A$ = "abcdeabcde"
PARTA$ = MID$(A$, 4)

...then the PARTA$ variable would be: 'deabcde'

LEN() Simply returns the value of the number of characters in a string of part of a string, when MID$() is used with it. For example:

A$ = "abcdeabcde"
X = LEN(A$)

In the above example, X = 10, as there are 10-characters in the string variable A$. If MID$() were used as in...

A$ = "abcdeabcde"
PARTA$ = MID$(A$, 4)

...then PARTA$ would equal 7.

INSTR() is also used for another purpose in this routine, which is to ignore end punctuation so that a search for 'can' will find and extract the word 'can.' from the sentence. This is accomplished by placing all the punctuation characters into a string and seeding that string parameter into the INSTR() statement as:

A$ = "abcde!"
X = INSTR(";,.!?: ", MID$(A$, LEN(A$), 1))

In this example, X will equal 4, as '!' is the 4th-character in the first parameter of INSTR$(). (Note, it is not measuring the placement of '!' in the string A$, which would be 6.) This is somewhat of a reverse way of using this statement, so it now becomes a true or false flag instead of just a counter.

X = 0 indicates no punctuation found

X > 0 indicates punctuation mark found.

Finally, Since I cap everything I code, newbies should know...

WORD$
SRCH$
MARKER%

...are all variables. All other words are QB reserved statements.

Pete

P.S. Pattycake was used because I stated it was a 'simple' example...OK, it's also an inside joke. For more details, contact the site administrator - lol.

-----------------------------------------------
Edited a missing ")" symbol in a code example:
x = INSTR(";,.!?: ", MID$(A$, LEN(A$), 1) edited to x = INSTR(";,.!?: ", MID$(A$, LEN(A$), 1))

Thanks Lisztfr,

Pete



    
This message has been edited by iorr5t on Aug 7, 2008 10:06 AM


 
 Respond to this message   
Responses

Newbies usually go to www.qbasic.com and click on The QBasic Forum
Forum regulars have their own ways, which include The QBasic Community Forums