Knowee
Questions
Features
Study Tools

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

🧐 Not the exact question you are looking for?Go ask a question

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.

This problem has been solved

Similar Questions

Write a program to check two strings are anagram or not.

An anagram is a word or a phrase made by transposing the letters of another word or phrase; for example, "parliament" is an anagram of "partial men," and "software" is an anagram of "swear oft." Write a program that figures out whether one string is an anagram of another string. The program should ignore white space and punctuation

Given a string S. the task is to determine if there exists another string that is an anagram of S.An anagram is a word or phrase formed by rearranging the letters of another word or phrase. For example "seekg" is an anagram of "geeks".Example 1:Input: S = "ab"Output: YESExplanation:The string "ba" is not identical to S and is an anagram of S

Given 2 strings, check if they are anagrams. An anagram is a rearrangement of the letters of one word to form another word. In other words, some permutations of string A must be the same as string B.Input FormatThe first line of input contains T - the number of test cases. It's followed by T lines, each line containing 2 space-separated strings.Output FormatFor each test case, print True/False, separated by a new line.Constraints10 points1 <= T <= 1001 <= len(S) <= 103'a' <= S[i] <= 'z'40 points1 <= T <= 1001 <= len(S) <= 105'a' <= S[i] <= 'z'ExampleInput4iamlordvoldemort tommarvoloriddleb hstop posthi heyOutputTrueFalseTrueFalse

The two strings don't contain the same number of a's and m's, so we print "Not Anagrams".

1/3

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.