Checking strings against each other (Anagrams)

后端 未结 23 1925
忘了有多久
忘了有多久 2020-11-30 10:55

The assignment is to write a program that accepts two groups of words from the user and then prints a \"True\" statement if the two are anagrams (or at least if all the lett

23条回答
  •  借酒劲吻你
    2020-11-30 11:19

    You could use the following code it will not count special characters nor it will count digits and will return "they are anagrams" if the total characters have occurred equally in both strings hence will tell the the strings are anagrams or not .

    text = input('Enter a string: ')
    text1 = input('Enter a string: ')
    text,text1 = text.lower(),text1.lower()
    count = 0
    count1=0
    for i in range(97,123):
        if chr(i) in text and chr(i) in text1:
        count1+=1
            if text.count(chr(i)) == text1.count(chr(i)):
                 count +=1
    if len(text) >= len(text1):
        num1 = len(text)
    else:
        num1 = len(text1)
    if count == count1:
        print("they are anagrams")
    else :
        print("they are not anagrams")
    

提交回复
热议问题