-
Notifications
You must be signed in to change notification settings - Fork 1
/
Anagram_Checker.c
52 lines (43 loc) · 958 Bytes
/
Anagram_Checker.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
#include <stdio.h>
#include <string.h>
int main(){
char s1[50];
char s2[50];
printf("Enter the First String : ");
fgets(s1, 100, stdin);
printf("Enter the Second String : ");
fgets(s2, 100, stdin);
char temp;
int i, j;
int n1 = strlen(s1);
int n2 = strlen(s2);
// If both strings are of different length, then they are not anagrams
if(n1 != n2) {
printf("\nThe Strings are not Anagrams \n");
return 0;
}
// Sort both strings
for (i = 0; i < (n1-1); i++){
for (j = (i+1); j < n1; j++){
if (s1[i] > s1[j]){
temp = s1[i];
s1[i] = s1[j];
s1[j] = temp;
}
if (s2[i] > s2[j]){
temp = s2[i];
s2[i] = s2[j];
s2[j] = temp;
}
}
}
// Compare both strings character by character
for(i = 0; i < n1; i++) {
if(s1[i] != s2[i]) {
printf("\nThe Strings are not Anagrams \n");
return 0;
}
}
printf("\nThe Strings are Anagrams \n");
return 0;
}