File tree 1 file changed +56
-0
lines changed
1 file changed +56
-0
lines changed Original file line number Diff line number Diff line change
1
+ // C++ program to check if two strings
2
+ // are anagrams of each other
3
+ #include < bits/stdc++.h>
4
+ using namespace std ;
5
+ #define NO_OF_CHARS 256
6
+
7
+ /* Function to check whether two
8
+ strings are anagram of each other */
9
+ bool areAnagram (char * str1, char * str2)
10
+ {
11
+ // Create 2 count arrays and initialize
12
+ // all values as 0
13
+ int count1[NO_OF_CHARS] = {0 };
14
+ int count2[NO_OF_CHARS] = {0 };
15
+ int i;
16
+
17
+ // For each character in input strings,
18
+ // increment count in the corresponding
19
+ // count array
20
+ for (i = 0 ; str1[i] && str2[i]; i++)
21
+ {
22
+ count1[str1[i]]++;
23
+ count2[str2[i]]++;
24
+ }
25
+
26
+ // If both strings are of different length.
27
+ // Removing this condition will make the
28
+ // program fail for strings like "aaca"
29
+ // and "aca"
30
+ if (str1[i] || str2[i])
31
+ return false ;
32
+
33
+ // Compare count arrays
34
+ for (i = 0 ; i < NO_OF_CHARS; i++)
35
+ if (count1[i] != count2[i])
36
+ return false ;
37
+
38
+ return true ;
39
+ }
40
+
41
+ // Driver code
42
+ int main ()
43
+ {
44
+ char str1[] = " fired" ;
45
+ char str2[] = " fried" ;
46
+
47
+ // Function Call
48
+ if (areAnagram (str1, str2))
49
+ cout <<
50
+ " The two strings are anagram of each other" ;
51
+ else
52
+ cout << " The two strings are not anagram of each "
53
+ " other" ;
54
+
55
+ return 0 ;
56
+ }
You can’t perform that action at this time.
0 commit comments