RosettaCodeData/Task/String-matching/BBC-BASIC/string-matching.basic

33 lines
1.1 KiB
Plaintext

first$ = "The fox jumps over the dog"
FOR test% = 1 TO 3
READ second$
starts% = FN_first_starts_with_second(first$, second$)
IF starts% PRINT """" first$ """ starts with """ second$ """"
ends% = FN_first_ends_with_second(first$, second$)
IF ends% PRINT """" first$ """ ends with """ second$ """"
where% = FN_first_contains_second_where(first$, second$)
IF where% PRINT """" first$ """ contains """ second$ """ at position " ; where%
howmany% = FN_first_contains_second_howmany(first$, second$)
IF howmany% PRINT """" first$ """ contains """ second$ """ " ; howmany% " time(s)"
NEXT
DATA "The", "he", "dog"
END
DEF FN_first_starts_with_second(A$, B$)
= B$ = LEFT$(A$, LEN(B$))
DEF FN_first_ends_with_second(A$, B$)
= B$ = RIGHT$(A$, LEN(B$))
DEF FN_first_contains_second_where(A$, B$)
= INSTR(A$, B$)
DEF FN_first_contains_second_howmany(A$, B$)
LOCAL I%, N% : I% = 0
REPEAT
I% = INSTR(A$, B$, I%+1)
IF I% THEN N% += 1
UNTIL I% = 0
= N%