Created
March 5, 2013 04:32
-
-
Save Battleroid/5088026 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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