Sep-26-2017, 07:43 AM
The task is to find a the first string as a substring in the other string.
For example, let's say we have:
string 1 = 'bob'
string 2 = 'bobsbugsbegone'
for i in s2:
s1 == to [b]ONLY[/b] [0:2] of s2
I want to write a code that detects when there's a substring in a string and that counts how many substrings are found. (the second part is easy)
In addition to that:
In the second try it also works
In the third try, i found out that it works only if the substring is at the beginning of the second string
For example, let's say we have:
string 1 = 'bob'
string 2 = 'bobsbugsbegone'
for i in s2:
s1 == to [b]ONLY[/b] [0:2] of s2
I want to write a code that detects when there's a substring in a string and that counts how many substrings are found. (the second part is easy)
This is my code: import sys s1 = sys.argv[1] s2 = sys.argv[2] k = 0 i = 0 def substrings(s1, s2, k): for k in s2: for n in range(len(s2)): if k == s1[n]: return True print('s1 is a substring in s2') else: print('s1 is not a substring in s2') return False print(substrings(s1, s2, k))Help?
In addition to that:
Error:(C:\Users\OmarHS\Miniconda3) c:\Users\OmarHS\Desktop\AUB\CMPS\200\ASST3>python substrings.py bob bobsbugsbegone
True
(C:\Users\OmarHS\Miniconda3) c:\Users\OmarHS\Desktop\AUB\CMPS\200\ASST3>python substrings.py bob robsbugsbegone
s1 is not a substring in s2
False
(C:\Users\OmarHS\Miniconda3) c:\Users\OmarHS\Desktop\AUB\CMPS\200\ASST3>python substrings.py bob robsbobsbegone
s1 is not a substring in s2
False
In the first try it worksIn the second try it also works
In the third try, i found out that it works only if the substring is at the beginning of the second string