-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask_24_08_19.c
More file actions
90 lines (73 loc) · 1.91 KB
/
Copy pathTask_24_08_19.c
File metadata and controls
90 lines (73 loc) · 1.91 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
Given two strings, check if they’re anagrams or not. Two strings are
anagrams if they are written using the same exact letters, ignoring
space, punctuation and capitalization. Each letter should have the same
count in both strings.
For example, ‘Eleven plus two’ and ‘Twelve plus one’ are meaningful
anagrams of each other.
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int anagrams(char string1[], char string2[])
{
int i,pt;
int arr[26], brr[26];
for(i = 0; i < 26; i++)
{
arr[i] = 0;
brr[i] = 0;
}
// if string lenght is not same then exit
if(strlen(string1) != strlen(string2))
{
printf("Given string is not anagrams\n");
exit(0);
}
// only check the chrater of string1 and store the in array of index
for(i = 0; string1[i] != '\0'; i++)
{
if(string1[i] <= 65 && string1[i] >= 91)
{
pt = string1[i] - 65;
arr[pt] = arr[pt] + 1;
}
if(string1[i] <= 97 && string1[i] >= 123)
{
pt = string1[i] - 97;
arr[pt] = arr[pt] + 1;
}
}
// only check the chrater of string2 and store the in array of index
for(i = 0; string2[i] != '\0'; i++)
{
if(string2[i] <= 65 && string2[i] >= 91)
{
pt = string2[i] - 65;
brr[pt] = brr[pt] + 1;
}
if(string2[i] <= 97 && string2[i] >= 123)
{
pt = string2[i] - 97;
brr[pt] = brr[pt] + 1;
}
}
for(i = 0; i < 26; i++)
{
if(arr[i] != brr[i])
return 0;
}
return 1;
}
int main()
{
char string1[100], string2[100];
printf("Enter the First string\n");
gets(string1);
printf("Enter the Second String\n");
gets(string2);
if(anagrams(string1, string2))
printf("Given string is anagrams\n");
else
printf("Given string is not anagrams\n");
}