-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc24.c
More file actions
43 lines (39 loc) · 917 Bytes
/
c24.c
File metadata and controls
43 lines (39 loc) · 917 Bytes
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
41
42
43
// Check if two strings are anagrams
#include <stdio.h>
int areAnagrams(const char *str1, const char *str2)
{
int count[256] = {0}; // ASCII character set
// Count characters in str1
for (int i = 0; str1[i] != '\0'; i++)
{
count[(unsigned char)str1[i]]++;
}
// Subtract counts using str2
for (int i = 0; str2[i] != '\0'; i++)
{
count[(unsigned char)str2[i]]--;
}
// Check if all counts are zero
for (int i = 0; i < 256; i++)
{
if (count[i] != 0)
{
return 0; // Not anagrams
}
}
return 1; // Anagrams
}
int main()
{
const char *str1 = "listen";
const char *str2 = "silent";
if (areAnagrams(str1, str2))
{
printf("\"%s\" and \"%s\" are anagrams.\n", str1, str2);
}
else
{
printf("\"%s\" and \"%s\" are not anagrams.\n", str1, str2);
}
return 0;
}