Count letter differences of two strings

后端 未结 11 705
既然无缘
既然无缘 2020-12-09 17:11

This is the behaviour I want:

a: IGADKYFHARGNYDAA
c: KGADKYFHARGNYEAA
2 difference(s).
11条回答
  •  生来不讨喜
    2020-12-09 17:35

    Here is my solution. This compares 2 strings, it doesn't matter what you put in A or B.

    #Declare Variables
    a='Here is my first string'
    b='Here is my second string'
    notTheSame=0
    count=0
    
    #Check which string is bigger and put the bigger string in C and smaller string in D
    if len(a) >= len(b):
        c=a
        d=b
    if len(b) > len(a):
        d=a
        c=b
    
    #While the counter is less than the length of the longest string, compare each letter.
    while count < len(c):
        if count == len(d):
            break
        if c[count] != d[count]:
            print(c[count] + " not equal to " + d[count])
            notTheSame = notTheSame + 1
        else:
            print(c[count] + " is equal to " + d[count])
        count=count+1
    
    #the below output is a count of all the differences + the difference between the 2 strings
    print("Number of Differences: " + str(len(c)-len(d)+notTheSame))
    

提交回复
热议问题