GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing codescracker.com — Python Program to Check If Two Strings Are Anagrams or Not
Python Program to Check If Two Strings Are Anagrams or NotThis article is created to cover some programs in Python that receive two strings from the user at run-time and check whether the two strings are anagrams or not. Here is a list of programs:
Before creating these programs, let's first understand anagrams. What are anagram strings?Two strings can be called an anagram when:
For example, abc and cba are two anagram strings. Similarly, creative and reactive are also anagrams. That is, characters from creative can be rearranged to form reactive, and vice versa. Check Two strings are anagrams using a for loopThis program is created using a for loop to check whether the two strings entered by the user are anagrams or not. The question is: write a Python program to check if an anagram exists or not without sorting the string. Here is its answer: print("Enter the First String:") textOne = str(input()) print("Enter the Second String:") textTwo = str(input()) found=0 notFound=0 lenOne = len(textOne) lenTwo = len(textTwo) if lenOne == lenTwo: for i in range(lenOne): found = 0 for j in range(lenOne): if textOne[i] == textTwo[j]: found = 1 break if found==0: notFound = 1 break if notFound==1: print("\nStrings are Not Anagram") else: print("\nStrings are Anagram") else: print("\nLength of Strings are not Equal") Here is its sample run: Now supply two strings input, say listen as the first string and silent as the second string, and press the ENTER key to check and print whether these two strings are anagrams or not, as shown in the snapshot given below: Here is another sample run with user input listen and silenx as the first and second strings: And here is the third sample run with user input codes and cracker as the first and second strings: Note: The str() method is used to convert any type of value to a string type. And the len() method returns the length of the string passed as its argument. The dry run of the above program with user input (listen and silent) goes like this:
Modified version of the previous programThis program uses the sorted() method to sort the string and compares strings in a character-by-character manner to check whether two strings are anagrams or not. The end= is used to skip inserting an automatic newline using print(). print(end="Enter the First String: ") textOne = str(input()) print(end="Enter the Second String: ") textTwo = str(input()) lenOne = len(textOne) lenTwo = len(textTwo) notFound=0 if lenOne == lenTwo: textOne = sorted(textOne) textTwo = sorted(textTwo) for i in range(lenOne): if textOne[i] != textTwo[i]: notFound=1 break if notFound==0: print("\nStrings are Anagram") else: print("\nStrings are Not Anagram") else: print("\nLength of Strings are not Equal") Here is its sample run with user input (creative and reactive) as two strings: Check anagram strings using sorted()This program uses the sorted() method to do the same job as the previous program did. The two strings entered by the user get compared directly using the == operator. print("Enter First String: ", end="") textOne = str(input()) print("Enter Second String: ", end="") textTwo = str(input()) lenOne = len(textOne) lenTwo = len(textTwo) if lenOne == lenTwo: if sorted(textOne) == sorted(textTwo): print("\nStrings are Anagram") else: print("\nStrings are Not Anagram") else: print("\nLength of Strings are not Equal") Here is its sample run with user input, sadder and dreads: Check anagram strings using the functionThis program is created using a user-defined function named CheckAnag(). The function receives two strings as its arguments and returns 1 if both strings are anagrams. def CheckAnag(sOne, sTwo): if sorted(sOne) == sorted(sTwo): return 1 print("Enter First String: ", end="") textOne = str(input()) print("Enter Second String: ", end="") textTwo = str(input()) rVal = CheckAnag(textOne, textTwo) if rVal==1: print("\nStrings are Anagram") else: print("\nStrings are Not Anagram") Check anagram strings using ClassThis is the last program created using classes, an object-oriented feature of Python. To access the member functions of a class, an object is required. Therefore, an object named obj is created of class CodesCracker to access its member function named CheckAnag() using the dot (.) operator. class CodesCracker: def CheckAnag(self, sOne, sTwo): if sorted(sOne) == sorted(sTwo): return 1 print("Enter First String: ", end="") textOne = str(input()) print("Enter Second String: ", end="") textTwo = str(input()) obj = CodesCracker() rVal = obj.CheckAnag(textOne, textTwo) if rVal==1: print("\nStrings are Anagram") else: print("\nStrings are Not Anagram") « Previous Program Next Program » Practice Quizzes
Test your programming and computer science knowledge with interactive MCQs and topic-based quizzes. Popular Topics
Explore the most popular programming and computer science tutorials on CodesCracker. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |