How to resolve the algorithm String matching step by step in the BASIC programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm String matching step by step in the BASIC programming language
Table of Contents
Problem Statement
Given two strings, demonstrate the following three types of string matching:
Optional requirements:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm String matching step by step in the BASIC programming language
Source code in the basic programming language
first$ = "qwertyuiop"
'Determining if the first string starts with second string
second$ = "qwerty"
IF LEFT$(first$, LEN(second$)) = second$ THEN
PRINT "'"; first$; "' starts with '"; second$; "'"
ELSE
PRINT "'"; first$; "' does not start with '"; second$; "'"
END IF
'Determining if the first string contains the second string at any location
'Print the location of the match for part 2
second$ = "wert"
x = INSTR(first$, second$)
IF x THEN
PRINT "'"; first$; "' contains '"; second$; "' at position "; x
ELSE
PRINT "'"; first$; "' does not contain '"; second$; "'"
END IF
' Determining if the first string ends with the second string
second$ = "random garbage"
IF RIGHT$(first$, LEN(second$)) = second$ THEN
PRINT "'"; first$; "' ends with '"; second$; "'"
ELSE
PRINT "'"; first$; "' does not end with '"; second$; "'"
END IF
10 A$ = "THIS, THAT, AND THE OTHER THING"
20 S$ = "TH"
30 DEF FN S(P) = MID$(A$, P, LEN(S$)) = S$
40 PRINT A$ : PRINT
110 S$(1) = "STARTS"
120 S$(0) = "DOES NOT START"
130 PRINT S$(FN S(1))" WITH "S$ : PRINT
210 R$ = "" : FOR I = 1 TO LEN(A$) - LEN(S$) : IF FN S(I) THEN R$ = R$ + STR$(I) + " "
220 NEXT I
230 IF LEN(R$) = 0 THEN PRINT "DOES NOT CONTAIN "S$
240 IF LEN(R$) THEN PRINT "CONTAINS "S$" LOCATED AT POSITION "R$
250 PRINT
310 E$(1) = "ENDS"
320 E$(0) = "DOES NOT END"
330 PRINT E$(FN S(LEN(A$) - LEN(S$) + 1))" WITH "S$
You may also check:How to resolve the algorithm Variables step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Formal power series step by step in the Kotlin programming language
You may also check:How to resolve the algorithm XML/Input step by step in the Sidef programming language
You may also check:How to resolve the algorithm Non-decimal radices/Input step by step in the Nim programming language
You may also check:How to resolve the algorithm Dot product step by step in the FunL programming language