Write a program that figures out whether one string is an anagram of another string. The program should ignore white space and punctuation
Question
Write a program that figures out whether one string is an anagram of another string. The program should ignore white space and punctuation.
Solution
Sure, here is a Python program that checks if two strings are anagrams of each other. This program ignores white space and punctuation.
import string
def is_anagram(s1, s2):
# Remove white spaces and convert to lower case
s1 = s1.replace(" ", "").lower()
s2 = s2.replace(" ", "").lower()
# Remove punctuation
s1 = s1.translate(str.maketrans('', '', string.punctuation))
s2 = s2.translate(str.maketrans('', '', string.punctuation))
# Check if sorted strings are equal
return sorted(s1) == sorted(s2)
# Test the function
print(is_anagram("Listen", "Silent")) # Should print: True
print(is_anagram("This is a string!", "Is this a string?")) # Should print: True
print(is_anagram("Hello", "World")) # Should print: False
This program works by first removing all white spaces from the input strings and converting them to lower case. Then it removes all punctuation from the strings. Finally, it checks if the sorted versions of the two strings are equal. If they are, it means one string is an anagram of the other.
Similar Questions
Given two strings A and B, find the minimum number of characters that need to be deleted from these 2 strings to make them anagrams of each other.
Problem:define a recursive function to generate all Anagrams of a given stringTest Case 1:Enter a word: abcabcacbbacbcacabcbaTest Case 2:Enter a word: ababba
Write a C program to read a sentence through keyboard and to display the count ofwhite spaces in the given sentence.
Write a program to check whether a given character is Alphabet or not using if else statement Note: Check for both upper and lower case characters
Write a Python program that matches a string that has an 'a' followed by anything, ending in 'b'
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.