Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 1

# (1) SubstringFinder is a function that takes in two parameters, (str1) &

(str2),
# (2) and returns the location(loc) ~integer~ of str2 in str1
# (3) [IF] str2 is a substring of str1
# To ensure that it is a substring, it checks that the [LENGTH] of str2 is
[LESS THAN OR EQUAL TO] that of str1
# (5) Then it compares 1 character of str1(char1) to 1 character of
str2(char2)
# (6) Using the [MID] function to pick one character each from str1 and str2
from positions(p1)(p2) respectively
# (7) [IF] char1 [IS EQUAL TO] char2
# (8) Increment p1 & P2 and ~loop~ [UNTIL] p2 [IS EQUAL TO] the length of str2
[OR] char1 [IS NOT EQUAL TO] char2
# (9) [ELSE] increment loc, update p1 and assign it the value of loc, reset p2
to 1
# (10) [IF] p2 [IS EQUAL TO] the length of str2, print loc

def SubstringFinder(str1, str2):


if len(str2) <= len(str1):

# (1) Substring Finder is a function that takes in two parameters, str1 & str2
# (2) and returns the location (variable1) ~integer~ of str2 in str1 [IF] str2
is a substring of str1
# (3) To achieve this, it first ensures the [LENGTHS] are compatible
# In other words, the length of str2 is [LESS THAN OR EQUAL TO] to that of
str1
# (4)It then compares each character of str2(char2) to each character of
str1(char1) ~in loop~ that runs[For](variable2)
# Where, (variable2) is 0 [TO] the length of str2
# (5) To compare, it uses the [MID] function

# ERRORS:
# The length of str2 should not be the upper bound because str2 might appear
later in str1, which could be much longer
# Instead, the loop should run for until the characters are not same or until
y is equal to length of str2+1

You might also like