forked from Vishal-Aggarwal0305/DSA-CODE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnagramString.c
62 lines (53 loc) · 1.23 KB
/
AnagramString.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
51
52
53
54
55
56
57
58
59
60
61
62
#include <stdio.h>
#include <string.h>
int main()
{
char swap;
char str1[100];
char str2[100];
printf("Enter First String\n");
fflush(stdin);
gets(str1);
printf("Enter second string\n");
fflush(stdin);
gets(str2);
int i, j, len1, len2;
len1 = strlen(str1);
len2 = strlen(str2);
// ---------length not equal ---------
if (len1 != len2)
{
printf("%s and %s is not an anagrams! \n", str1, str2);
return 0;
}
//making both string sorted
for (i = 0; i < len1 - 1; i++)
{
for (j = i + 1; j < len1; j++)
{
if (str1[i] > str1[j])
{
swap = str1[i];
str1[i] = str1[j];
str1[j] = swap;
}
if (str2[i] > str2[j])
{
swap = str2[i];
str2[i] = str2[j];
str2[j] = swap;
}
}
}
// Now both strings are sorted so comparing both strings character by character
for (i = 0; i < len1; i++)
{
if (str1[i] != str2[i])
{
printf("Not anagram \n", str1, str2);
return 0;
}
}
printf(" Yes they are anagram \n");
return 0;
}