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
Anagrams are the two different words formed with same characters: For eg: EAT and TEA likewise there can be numerous examples.
One good way to see if give two words or sentences are anagrams is to set a counter array of size 256, and initially set all the values to 0. (This can be a good option if the input is bigger, at least than a few words) Now start reading the first string(word or a sentence), and increment its corresponding ASCII location in the array by one. Repeat this for the complete string. Now start reading the second string and keep decreasing the corresponding ASCII counter of each letter in the array. Finally, parse the array; if all the values are zero then the inputs were anagrams otherwise not. Following is the commented code for the better understanding.
#include<iostream>
#include<string>
using namespace std;
bool is_anagram(string s1, string s2)
{
//Following statement chechs the base condition; if either of the strings is empty,
//return False
if(s1.length() == 0 || s2.length() == 0)
return false;
//initializing the counter array and setting it's values to 0
int counter[256] = {0};
//Calculating the lengths of both the strings
int len1 = s1.length();
int len2 = s2.length();
//Following is also a base condition that checks whether the strings are equal in
//length, if not we return False
if(len1 != len2)
return false;
//Following for loop increments the values of the counter array for the first
//string
for(int i = 0; i < len1; i++)
{
counter[s1[i]]++;
}
//This for loop decrements the values of the counter array for the second string
for(int i = 0; i < len2; i--)
{
counter[s2[i]]--;
}
//Now we check whether the counter array is empty/(or as it was initialized); if
//yes then the two strings are anagrams
for(int i = 0; i < 256; i++)
{
if(counter[i] != 0)
return false;
}
return true;
}
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")
You need to think through your conditional logic a bit more. The loop is on the right track, but if there is a letter in s1 that is NOT in s2, you should break
out of this loop and print the "False" statement. Consider using a variable like all_s1_in_s2 = True
and then setting that to false if you find a letter that doesn't match.
Some other tips:
for l in s1
will loop through string s1 giving you access to each letter in sequence as l
- you don't need range
or len
at all
The if .. in
statement can help test whether a letter exists in a string, e.g. if letter in mystring:
is a valid statement and this could help you a lot, again not needing range
or len
You should avoid using numbers in variable names where possible - better would be word_one
and word_two
, as an example
str1='ohaha'
str2='hahao1'
set3=set(str1)
set4=set(str2)
if(len(set3.difference(set4))==0 and len(set4.difference(set3))==0):
print('anagrams')
else:
print('not anagrams')
You can use the magic Counter from collections library. From documentation:
It is an unordered collection where elements are stored as dictionary keys and their counts are stored as dictionary values
So, you can initialize a Counter object with a string (a iterable) and compare with another Counter from a string
from collections import Counter
def is_anagram(str1, str2):
return Counter(str1) == Counter(str2)