First declare the main text
text=”abcd” #this is main text
Noe declare the pattern you want to search in the main text
patt=”cd” #this is pattern to search for in the main text
Now find the difference in length of main text and pattern
#first calcualte the difference between main text and pattern
diff=len(text)-len(patt)
print(diff)
#now loop over this number and keep on comparing each character in the text with each character in the pattern
for i in range(0,diff):
j=0
#keep on incrementing j till both characters match
while j<len(patt) and text[i+j]==patt[j]:
j+=1;
if(j==len(patt)):
print(i)
print('not found')
Leave a Reply