-
Notifications
You must be signed in to change notification settings - Fork 91
/
13.py
36 lines (28 loc) · 950 Bytes
/
13.py
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
# Python code to count number of matching
# characters in a pair of strings
# count function
def count(str1, str2):
c, j = 0, 0
# loop executes till length of str1 and
# stores value of str1 character by character
# and stores in i at each iteration.
for i in str1:
# this will check if character extracted from
# str1 is present in str2 or not(str2.find(i)
# return -1 if not found otherwise return the
# starting occurrence index of that character
# in str2) and j == str1.find(i) is used to
# avoid the counting of the duplicate characters
# present in str1 found in str2
if str2.find(i) >= 0 and j == str1.find(i):
c += 1
j += 1
print('No. of matching characters are : ', c)
# Main function
def main():
str1 = 'aabcddekll12@' # first string
str2 = 'bb2211@55k' # second string
count(str1, str2) # calling count function
# Driver Code
if __name__ == "__main__":
main()