Im trying to take in two strings and send them to a function that tells if they are anagrams spaces and punctuation is ignored but can be input. I am either not calling my function right or the function itself is messed up. Code is a disaster i know im just trying to get this to work
griffingradke, your original code doesn't work - it doesn't search for anagrams. It can't be "improved" so much as "destroyed because it's doing the wrong thing". Your original code is checking for palindromes.
something like this:
clean up the string (remove anything not in 'a' to 'z' for english for example, including spaces) and set it to either upper or lower case letters across the board.
then call sort on the string.
repeat for second string.
compare a==b. if they do, it is an anagram. Not sure if anagrams require distinct letters or not, if not, you should deduplicate both before compare. (eg does AABBCC == ABC?)
this is like 4 lines for each string if you use c++ <algorithm>s.
something like: (its a lot to type in without checking it, but hopefully close)
1 2 3 4
std::transform(s.begin(), s.end(),s.begin(), ::toupper); //upper case input
s.erase(std::remove_if(s.begin(), s.end(), //cleanup
[](char &c){return (c>'Z'||c<'A');}),s.end());
std::sort(s.begin(),s.end()); //sort the string
Not clear if you can use the <string> library or not. You use strings, but do not include the <string> header which is required if you want to use strings.
If you're allowed to use strings, you can replace lines 11-21 with:
1 2
alen = a.size();
blen = b.size();
Both lastchance and jonin have given you the general logic to determine if two strings are anagrams. Sort each string, them compare the results for equality.
Sorting is convenient if you have a pre-written function. Other methods are easier than writing a sorting function from scratch, IMO.
Two anagrams have the same frequencies each letter. Build a frequency table of the letters in each work and compare them to see if they're the same.
1 2 3 4 5 6 7 8 9 10 11
bool anagrams(string a, string b)
{
int aFreq[26] = {0}, bFreq[26] = {0};
//you fill in the rest.
for (int i = 0; i < 26; i++)
if (aFreq[i] != bFreq[i])
returnfalse;
returntrue;
}
Nice, Brownie. I knew there had to be another alternative to sorting the string itself, but I was having a mental block as to what the alternative was. That's probably what OP wants if he isn't expected to know how to sort yet. And that's linear "O(n)" instead of "O(n log n)", as well, I think. (Well, O(n * k) where k is the number of letters in your alphabet, haha).
and the bucket sort works both ways. you can compare all the elements against true, and you'll have the duplicates ignored match; if you compare the tables exactly, you have the exact match version. you can also clean and sort and everything in one pass with a more convoluted algorithm. One way to do that is to use the bucket idea on a 256 sized ascii array, load it via bucket[toupper(string[i])] and then compare memcpy( from 'A' to 'Z' only of the array).