Use nested loops to write a function called substring(s,b) that returns True or False depending on whether or not b is a substring of s. A substring is a series of characters which appears inside another word in the exact same order. For example, substring(“banana”, “ana”) should return True whereas substring(“banana”,”bna”) should return False. Use this function to write a program that asks the user for a string s and a substring b and prints whether b is a substring of s.
For this question, you may not use the predefined find() and in functions nor any other functions from the String library (i.e. you must write a nested loop).
A sample session of your program should run as follows (this function called from IDLE):
SAMPLE 1
>>> substring("banadaa","nad")
True
SAMPLE 2
>>> substring("banadaa","bnad")
False
PROGRAM::
def
substring(string,substring):
for i in range (0, len(string)):
if string[i]==substring[0]:
sum =0
for j in range(0, len(substring)):
if string[i+j] != substring[j]:
break
sum = 0
else:
sum = sum +1
if sum == len(substring):
return True
return False
print(substring("banadaa","nad"))
print(substring("banadaa","bnad"))
SCREENSHOT::
Get Answers For Free
Most questions answered within 1 hours.