Jump to content

Anagram C++ Help


Scripts

Recommended Posts

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;
}
Link to comment
Share on other sites

I'm no C++ guru, but first of all I would suggest you replace all ! ~ $ % and etc characters to nothing. Then as far as I understand 

histogram1 != histogram2

are two different arrays so they will never be equal even when the values are the same. Just loop though one of them and check if the second one has that letter and the same amount of letters as the first one. If you don't find at least one match - instantly break the loop.

Link to comment
Share on other sites

I won't give it all away, besides most stuff you're asking is freely available on google. A neat way to check whether a char is not a symbol (or more importantly, is a letter in the alphabet) is to do

    if( (l>='a' && l<='z') || (l>='A' && l<='Z'))
        printf("%c is a letter.", l);
    else
        printf("%c is not a letter.", l);

I've got to head off now but I can edit this post with more info on other bits as well a bit later!

Edit:

as nosepicker said, you cannot compare arrays using the standard operator like that in c (or for the most part any language). For example, in java you must use .equals() which is itself a method specified for whichever object you are dealing with, overridden from parent classes.

You'll have to iterate through both arrays, perhaps best done with a function, like so:

bool compareArrays(int a[], int b[], int n) {
  for(int i = 1; i <= n; i++)
    if (a[i] != b[i]) return false;
  return true;
}

where your two arrays are a and b, and n is the length of both arrays. Make sure both arrays really are length n before sending them in! :)

Edit 2: if you need any more help or wish for me to write an example program to solve this problem for you, just let me know!

~apa

Edited by Apaec
  • Like 3
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...