Find a string - Python Solution

Find a string - Python Solution

Instructor-svgAl-Mamun Sarkar
Mar 23 , 2020

Find a string - Python Solution. Click here to see the problem.

Code:


def count_substring(string, sub_string):
    count = 0
    for i in range(0, len(string) - len(sub_string) + 1):
        if string[i] == sub_string[0]:
            flag = 1
            for j in range(0, len(sub_string)):
                if string[i+j] != sub_string[j]:
                    flag = 0
                    break
            if 1 == flag:
                count += 1
    return count


if __name__ == '__main__':
    string = input().strip()
    sub_string = input().strip()

    count = count_substring(string, sub_string)
    print(count)

Test Input:

ABCDCDC
CDC

Test Output:

2
  • Share On:
  • fb
  • twitter
  • pinterest
  • instagram