forked from AkshayNachappa/Hacktoberfest_101
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Anagram.c
50 lines (46 loc) · 1.03 KB
/
Anagram.c
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
44
45
46
47
48
49
50
#include <stdio.h>
#include <string.h>
int check_anagram(char word1[], char word2[])
{
int n1 = strlen(word1);
int n2 = strlen(word2);
//Strings of different length cannot be anagram
if (n1 != n2)
return -1;
//Sorting both strings
int i, j;
char temp;
for (i = 0; i < n1 - 1; i++)
{
for (j = i + 1; j < n1; j++)
{
if (word1[i] > word1[j])
{
temp = word1[i];
word1[i] = word1[j];
word1[j] = temp;
}
if (word2[i] > word2[j])
{
temp = word2[i];
word2[i] = word2[j];
word2[j] = temp;
}
}
}
//Check for anagram
if (strcmp(word1, word2) == 0)
return 0;
else
return -1;
}
int main()
{
char s1[] = "earth";
char s2[] = "heart";
if (check_anagram(s1, s2) == 0)
printf("Strings are Anagrams!\n");
else
printf("Strings are not Anagrams! \n");
return 0;
}