-
Notifications
You must be signed in to change notification settings - Fork 0
/
anagramchecker.cpp
52 lines (41 loc) · 1.09 KB
/
anagramchecker.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string>
using namespace std;
bool isanagram(string str1,string str2);
int main(){
string str1, str2;
str1="Arma";
str2="mary";
if ( isanagram(str1,str2) )
cout << " Strings are an anagarm" << endl;
else
cout<< " Strings are not anagram" <<endl;
}
bool isanagram(string str1,string str2){
int char_marker[26];
for ( int i=0;i<26;i++ )
char_marker[i]=0;
if ( str1.length()==str2.length() )
{
string::iterator it1;
string::iterator it2;
it2=str2.begin();
for ( it1=str1.begin() ; it1 < str1.end(); it1++, it2++ )
{
if( (int)(*it1)<90 )
char_marker[(int)(*it1)-65]++;
else
char_marker[(int)(*it1)-97]++;
if( (int)(*it2)<90 )
char_marker[(int)(*it2)-65]--;
else
char_marker[(int)(*it2)-97]--;
}
for ( int i=0;i<26;i++ )
if ( char_marker[i]!=0 )
return false;
return true;
}
else
return false;
}