forked from rathoresrikant/HacktoberFestContribute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathare_anagrams.cpp
41 lines (31 loc) · 951 Bytes
/
are_anagrams.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <algorithm>
#include <cctype>
bool is_anagram(std::string& first, std::string& second) {
if (first.size() != second.size())
return false;
std::transform(first.begin(), first.end(), first.begin(), tolower);
std::transform(second.begin(), second.end(), second.begin(), tolower);
int count[255] = { 0 };
for (auto c : first)
count[static_cast<int>(c)]++;
for (auto c : second) {
if (!count[static_cast<int>(c)]) {
return false;
} else {
count[static_cast<int>(c)]--;
}
}
for (auto i : count)
if (i != 0) return false;
return true;
}
int main() {
std::string first, second;
std::cout << "Enter the first string: ";
std::cin >> first;
std::cout << "Enter the second string: ";
std::cin >> second;
std::cout << "Result: " << is_anagram(first, second) << std::endl;
return 0;
}