I am to write a program that will take 2 strings, put them into a function called "build_histogram" and build a int array that keeps count of how many times each letter appears in each string, then compare the arrays and if they are equal, then they are anagrams. The instructions state we are to ignore all symbols(ex. !, whitespaces, _, etc.) and it is not to be case sensitive.
This is my code now...it counts the number of characters correctly in each string, the only issue now is the "if else" statement at the bottom still doesn't recognize that the arrays are the same, also it's having a hard time when symbols like '!' are in the strings.
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
void build_histogram(int letters[], string s) {
for(int i = 0; i < s.length(); i++) {
char currLetter = s[i];
currLetter = tolower(currLetter);
int index = currLetter - 97;
letters[index]++;
}
}
int main()
{
string s1, s2;
int histogram1[26] = {0};
int histogram2[26] = {0};
cout << "Enter two strings." << endl;
getline(cin, s1);
getline(cin, s2);
build_histogram(histogram1, s1);
build_histogram(histogram2, s2);
if (histogram1 != histogram2) {
cout << "They are not anagrams." << endl;
} else {
cout << "They are anagrams!" << endl;
}
return 0;
}