-1

I want to make program which counts the number of given substring in a string.

s = 'azcbobobegghakl'
substring = "bob"

for i in range(0, len(s)):
    if substring in s:
        j = 0
        count = 0
        count1 = 0
        while s[i] == substring[j]:
            count += 1
            i += 1
            j += 1
            if j > len(substring):
                break
        if count == len(substring):
            count1 += 1
            count = 0
        else:
            i += 1

print(count1)
Taohidul Islam
  • 5,246
  • 3
  • 26
  • 39

1 Answers1

0
import re
s = 'azcbobobegghakl'
searchSub = 'bob'
print(len(re.findall('(?=' + searchSub + ')',s)))

OUTPUT:

2
DirtyBit
  • 16,613
  • 4
  • 34
  • 55