-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforsubstrinstr.py
More file actions
65 lines (39 loc) · 803 Bytes
/
forsubstrinstr.py
File metadata and controls
65 lines (39 loc) · 803 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# print how many times substring is present in given string
s=input('enter a string:') #s='bangalore'
ss=input('enter a substring:') #ng
count=0
for ip in range(len(s)):
if ss==s[ip:ip+len(ss):1]:
count+=1
print(count)
'''
Initial variables:
count = 0
Iteration through string s:
ip = 0:
s[0:2] → "ba"
"ng" == "ba"? No → count = 0
ip = 1:
s[1:3] → "an"
"ng" == "an"? No → count = 0
ip = 2:
s[2:4] → "ng"
"ng" == "ng"? Yes → count = 0 + 1 = 1
ip = 3:
s[3:5] → "ga"
"ng" == "ga"? No → count = 1
ip = 4:
s[4:6] → "al"
"ng" == "al"? No → count = 1
ip = 5:
s[5:7] → "lo"
"ng" == "lo"? No → count = 1
ip = 6:
s[6:8] → "or"
"ng" == "or"? No → count = 1
ip = 7:
s[7:9] → "re"
"ng" == "re"? No → count = 1
Final Output:
Print count → 1
'''