Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2597f0c

Browse files
authored
Create 44 Check anagram.cpp
1 parent d816394 commit 2597f0c

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

‎Array/Problems/44 Check anagram.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// C++ program to check if the strings are anagrams
2+
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
// function to check whether two strings are anagram of each other
7+
bool areAnagram(string str1, string str2)
8+
{
9+
// Get lengths of both strings
10+
int n1 = str1.length();
11+
int n2 = str2.length();
12+
13+
// If length of both strings is not same, then they
14+
// cannot be anagram
15+
if (n1 != n2)
16+
return false;
17+
18+
// Sort both the strings
19+
sort(str1.begin(), str1.end());
20+
sort(str2.begin(), str2.end());
21+
22+
// Compare sorted strings
23+
for (int i = 0; i < n1; i++)
24+
if (str1[i] != str2[i])
25+
return false;
26+
27+
return true;
28+
}
29+
30+
int main()
31+
{
32+
string str1;
33+
string str2;
34+
cout << "\nInput the strings : ";
35+
cin >> str1 >> str2;
36+
if (areAnagram(str1, str2))
37+
cout << "The two strings are anagram of each other";
38+
else
39+
cout << "The two strings are not anagram of each other";
40+
41+
return 0;
42+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /