From f2eac4513ede60422829e3a0016e7f6d171fa774 Mon Sep 17 00:00:00 2001 From: ysumitsingh159 <91625202+ysumitsingh159@users.noreply.github.com> Date: Tue, 4 Oct 2022 12:56:12 +0530 Subject: [PATCH] hacktoberfest-2022 --- String/anagram_check.cpp | 46 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 String/anagram_check.cpp diff --git a/String/anagram_check.cpp b/String/anagram_check.cpp new file mode 100644 index 0000000..d287b1a --- /dev/null +++ b/String/anagram_check.cpp @@ -0,0 +1,46 @@ +// C++ program to check whether two strings are anagrams +// of each other +#include +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 = "gram"; + string str2 = "arm"; + + // 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; +} +