104k views
1 vote
Write a program in python that compares two strings given as input. Output the number of characters that match in each string position. The output should use the correct verb (match vs matches) according to the character count.

Ex: If the input is:
crash
crush
the output is:
4 characters match
Ex: If the input is:
cat
catnip
the output is:
3 characters match
Ex: If the input is:
mall
saw
the output is:
1 character matches
Ex: If the input is:
apple
orange
the output is:
0 characters match

User Alex Aza
by
8.4k points

1 Answer

3 votes

Answer: Here You Go

string1 = input("Enter the first string: ")

string2 = input("Enter the second string: ")

# initialize a counter variable to keep track of the number of matching characters

match_count = 0

# iterate over the characters in the strings

for i in range(min(len(string1), len(string2))):

if string1[i] == string2[i]:

match_count += 1

# output the results using the correct verb based on the number of matching characters

if match_count == 1:

print("There is 1 character that matches in both strings.")

else:

print("There are " + str(match_count) + " characters that match in both strings.")

Step-by-step explanation:

User Sergii Pechenizkyi
by
7.3k points