From af8cfc21c2bb3b5a2fb917db2200e849f04b0106 Mon Sep 17 00:00:00 2001 From: Jeevesh Joshi <64859843+Jeevesh-Joshi@users.noreply.github.com> Date: Sat, 3 Oct 2020 22:04:58 +0530 Subject: [PATCH] Anagram.cpp Check for Anagram of string. --- anagram.cpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 anagram.cpp diff --git a/anagram.cpp b/anagram.cpp new file mode 100644 index 0000000..9752be1 --- /dev/null +++ b/anagram.cpp @@ -0,0 +1,36 @@ +#include +using namespace std; + +bool Anagram(string str1, string str2) +{ + int n1 = str1.length(); + int n2 = str2.length(); + + if (n1 != n2) + return false; + + sort(str1.begin(), str1.end()); + sort(str2.begin(), str2.end()); + + for (int i = 0; i < n1; i++) + if (str1[i] != str2[i]) + return false; + + return true; +} + +int main() +{ + string str1; + string str2; + cout<<"Enter the first string : "; + cin>>str1; + cout<<"Enter the second string : "; + cin>>str2; + if (Anagram(str1, str2)) + cout << "The two strings are anagram of each other"; + else + cout << "The two strings are not anagram of each other"; + + return 0; +} \ No newline at end of file