Skip to content

Commit d1c45ac

Browse files
committed
Check Two Strings are Anagrams
1 parent b38fc04 commit d1c45ac

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,7 @@ This is a repository containing various C++ Programs to understand the basic con
142142

143143
C++ Code for checking whether binary representation of a number is palindrome or not.
144144

145+
* [Check whether two strings are anagrams of each other](https://github.com/altruistcoder/Data-Structures/blob/master/check_anagrams.cpp):
146+
147+
C++ Code for checking whether two given strings are anagrams of each other.
148+

check_anagrams.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <iostream>
2+
#include <string.h>
3+
using namespace std;
4+
5+
bool checkAnagrams(char st1[], char st2[])
6+
{
7+
int i, l1 = strlen(st1), res=0;
8+
if(l1 != strlen(st2))
9+
return false;
10+
for(i=0; i<l1; i++)
11+
{
12+
res = res ^ (int)st1[i];
13+
res = res ^ (int)st2[i];
14+
}
15+
return res==0;
16+
}
17+
18+
int main()
19+
{
20+
char s1[100], s2[100];
21+
cout<<"Enter the two strings to be checked for being anagrams: ";
22+
cin>>s1>>s2;
23+
if(checkAnagrams(s1, s2))
24+
cout<<"The given two strings are anagrams of each other\n";
25+
else
26+
cout<<"The given two strings are not anagrams of each other\n";
27+
return 0;
28+
}

0 commit comments

Comments
 (0)