Skip to content

Commit 673b44b

Browse files
authored
Anagram-Program-in-C
This is he code to check whether string is anagram. Anagram is a word, phrase, or name formed by rearranging the letters of another, such as spar, formed from rasp.
1 parent 104040a commit 673b44b

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

Anagram-Program-in-C

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <stdio.h>
2+
3+
int check_anagram(char [], char []);
4+
5+
int main()
6+
{
7+
char a[100], b[100];
8+
9+
printf("Enter a string\n");
10+
gets(a);
11+
12+
printf("Enter a string\n");
13+
gets(b);
14+
15+
if (check_anagram(a, b) == 1)
16+
printf("The strings are anagrams.\n");
17+
else
18+
printf("The strings aren't anagrams.\n");
19+
20+
return 0;
21+
}
22+
23+
int check_anagram(char a[], char b[])
24+
{
25+
int first[26] = {0}, second[26] = {0}, c=0;
26+
27+
// Calculating frequency of characters of first string
28+
29+
while (a[c] != '\0')
30+
{
31+
first[a[c]-'a']++;
32+
c++;
33+
}
34+
35+
c = 0;
36+
37+
while (b[c] != '\0')
38+
{
39+
second[b[c]-'a']++;
40+
c++;
41+
}
42+
43+
// Comparing frequency of characters
44+
45+
for (c = 0; c < 26; c++)
46+
{
47+
if (first[c] != second[c])
48+
return 0;
49+
}
50+
51+
return 1;
52+
}

0 commit comments

Comments
 (0)