-3

im just learning how to program on edx, and theres this question that i just can seem to understand not to even talk of solving. please i need solutions and idea on how to understand the question and implement it

  • QUESTION-

Assume s is a string of lower case characters.

Write a program that prints the number of times the string 'bob' occurs in s. For example, if s = 'azcbobobegghakl', then your program should print

Number of times bob occurs is: 2

1 Answers1

0

Since you are just learning, start with basic method.

Simplest apporch will be start from the begining consider all the substrings of the length of the substring that is needed to be counted. If there is a match increase the count.

You can refer the following code to get an idea.

s = 'azcbobobegghakl'
sb = 'bob'

results = 0
sub_len = len(sb)

for i in range(len(s)):
    if s[i:i+sub_len] == sb:
        results += 1

print(results)
Sreeram TP
  • 11,346
  • 7
  • 54
  • 108