|
1 | | -#include <stdio.h> |
2 | | - |
3 | | -int check_anagram(char [], char []); |
4 | | - |
| 1 | +#include <stdio.h> |
| 2 | +#include <string.h> |
| 3 | +#include <stdlib.h> |
| 4 | + |
| 5 | +int checkAnagram(char *str1, char *str2); |
| 6 | + |
5 | 7 | int main() |
6 | 8 | { |
7 | | - char a[100], b[100]; |
8 | | - |
9 | | - printf("Enter a string\n"); |
10 | | - gets(a); |
| 9 | + char str1[100], str2[100]; |
| 10 | + |
| 11 | + printf("Function : whether two given strings are anagram :"); |
| 12 | + printf("\nExample : pears and spare, stone and tones :"); |
| 13 | + |
| 14 | + printf(" Input the first String : "); |
| 15 | + fgets(str1, sizeof str1, stdin); |
| 16 | + printf(" Input the second String : "); |
| 17 | + fgets(str2, sizeof str2, stdin); |
11 | 18 |
|
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; |
| 19 | + if(checkAnagram(str1, str2) == 1) |
| 20 | + { |
| 21 | + str1[strlen(str1)-1] = '\0'; |
| 22 | + str2[strlen(str2)-1] = '\0'; |
| 23 | + printf(" %s and %s are Anagram.\n\n",str1,str2); |
| 24 | + } |
| 25 | + else |
| 26 | + { |
| 27 | + str1[strlen(str1)-1] = '\0'; |
| 28 | + str2[strlen(str2)-1] = '\0'; |
| 29 | + printf(" %s and %s are not Anagram.\n\n",str1,str2); |
| 30 | + } |
| 31 | + return 0; |
21 | 32 | } |
22 | 33 |
|
23 | | -int check_anagram(char a[], char b[]) |
| 34 | + |
| 35 | +//Function to check whether two passed strings are anagram or not |
| 36 | + |
| 37 | +int checkAnagram(char *str1, char *str2) |
24 | 38 | { |
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; |
| 39 | + int str1ChrCtr[256] = {0}, str2ChrCtr[256] = {0}; |
| 40 | + int ctr; |
| 41 | + |
| 42 | + /* check the length of equality of Two Strings */ |
| 43 | + |
| 44 | + if(strlen(str1) != strlen(str2)) |
| 45 | + { |
| 46 | + return 0; |
| 47 | + } |
| 48 | + |
| 49 | + //count frequency of characters in str1 |
| 50 | + |
| 51 | + for(ctr = 0; str1[ctr] != '\0'; ctr++) |
| 52 | + { |
| 53 | + str1ChrCtr[str1[ctr]]++; |
| 54 | + } |
| 55 | + |
| 56 | + //count frequency of characters in str2 |
| 57 | + |
| 58 | + for(ctr = 0; str2[ctr] != '\0'; ctr++) |
| 59 | + { |
| 60 | + str2ChrCtr[str2[ctr]]++; |
| 61 | + } |
| 62 | + |
| 63 | + //compare character counts of both strings |
| 64 | + |
| 65 | + for(ctr = 0; ctr < 256; ctr++) |
| 66 | + { |
| 67 | + if(str1ChrCtr[ctr] != str2ChrCtr[ctr]) |
| 68 | + return 0; |
| 69 | + } |
| 70 | + return 1; |
52 | 71 | } |
0 commit comments