Skip to content

Instantly share code, notes, and snippets.

@Battleroid
Created March 5, 2013 04:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Battleroid/5088026 to your computer and use it in GitHub Desktop.
Save Battleroid/5088026 to your computer and use it in GitHub Desktop.
#ifndef ANAGRAM_H
#define ANAGRAM_H
#include <string>
#define NO_OF_CHARS 256
using namespace std;
bool isAnagram (const string& a, const string& b) {
int count[NO_OF_CHARS] = {0};
int i;
for (i = 0; a[i] && b[i]; i++)
{
count[a[i]]++;
count[b[i]]--;
}
if (a[i] || b[i])
return false;
for (i = 0; i < NO_OF_CHARS; i++)
if (count[i])
return false;
return true;
}
#endif
#include "Anagram.h"
#include <iostream>
#include <string>
using namespace std;
int main () {
string a, b;
printf("Enter a string 'a': ");
cin >> a;
printf("Enter a string 'b': ");
cin >> b;
// test if they are anagrams
if (isAnagram(a, b))
cout << a << " and " << b << " are anagrams." << endl;
else
cout << a << " and " << b << " are not anagrams." << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment