forked from tarunsinghofficial/HacktoberFest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
36edc92
commit 127fb33
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// C++ program to check whether two strings are anagrams | ||
// of each other | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
/* function to check whether two strings are anagram of | ||
each other */ | ||
bool areAnagram(string str1, string str2) | ||
{ | ||
// Get lengths of both strings | ||
int n1 = str1.length(); | ||
int n2 = str2.length(); | ||
|
||
// If length of both strings is not same, then they | ||
// cannot be anagram | ||
if (n1 != n2) | ||
return false; | ||
|
||
// Sort both the strings | ||
sort(str1.begin(), str1.end()); | ||
sort(str2.begin(), str2.end()); | ||
|
||
// Compare sorted strings | ||
for (int i = 0; i < n1; i++) | ||
if (str1[i] != str2[i]) | ||
return false; | ||
|
||
return true; | ||
} | ||
|
||
// Driver code | ||
int main() | ||
{ | ||
string str1 = "test"; | ||
string str2 = "ttew"; | ||
|
||
// Function Call | ||
if (areAnagram(str1, str2)) | ||
cout << "The two strings are anagram of each other"; | ||
else | ||
cout << "The two strings are not anagram of each " | ||
"other"; | ||
|
||
return 0; | ||
} |