Skip to content

Commit 90ca772

Browse files
Merge pull request #65 from Billsfriend/exercism-sync/f87f5eb2aa56756f
[Sync Iteration] c/anagram/1
2 parents aa2632c + 6cdf6c1 commit 90ca772

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

solutions/c/anagram/1/anagram.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include "anagram.h"
2+
#include <stddef.h>
3+
#include <string.h>
4+
#include <stdbool.h>
5+
#include <ctype.h>
6+
7+
static bool is_anagram(const char *subject, const char *candidate) {
8+
size_t len = strlen(subject);
9+
// Count letter frequencies in subject
10+
int subject_count[26] = {0};
11+
for (size_t i = 0; i < len; i++) {
12+
char c = tolower(subject[i]);
13+
if (c >= 'a' && c <= 'z') {
14+
subject_count[c - 'a']++;
15+
}
16+
}
17+
18+
// Subtract letter frequencies from candidate
19+
for (size_t i = 0; i < len; i++) {
20+
char c = tolower(candidate[i]);
21+
if (c >= 'a' && c <= 'z') {
22+
subject_count[c - 'a']--;
23+
}
24+
if (subject_count[c - 'a'] < 0) {
25+
return false;
26+
}
27+
}
28+
29+
return true;
30+
}
31+
32+
/**
33+
* @description - determines if any of the words in candidate are anagrams
34+
* for subject. Contents of candidate structures may be modified.
35+
*/
36+
void find_anagrams(const char *subject, struct candidates *candidates) {
37+
size_t len = strlen(subject);
38+
for (size_t i = 0; i < candidates->count; i++) {
39+
if (candidates->candidate[i].is_anagram == UNCHECKED) {
40+
// check word length to avoid index out of bounds
41+
if (len != strlen(candidates->candidate[i].word)) {
42+
candidates->candidate[i].is_anagram = NOT_ANAGRAM;
43+
continue;
44+
}
45+
// identical words are not anagrams
46+
size_t j = 0;
47+
for (; j < len; j++) {
48+
if (tolower(subject[j]) != tolower(candidates->candidate[i].word[j])) {
49+
break;
50+
}
51+
}
52+
if (j == len) {
53+
candidates->candidate[i].is_anagram = NOT_ANAGRAM;
54+
} else {
55+
// check if the words are anagrams
56+
if (is_anagram(subject, candidates->candidate[i].word)) {
57+
candidates->candidate[i].is_anagram = IS_ANAGRAM;
58+
} else {
59+
candidates->candidate[i].is_anagram = NOT_ANAGRAM;
60+
}
61+
}
62+
}
63+
}
64+
}

solutions/c/anagram/1/anagram.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#ifndef ANAGRAM_H
2+
#define ANAGRAM_H
3+
4+
#include <stddef.h>
5+
6+
#define MAX_STR_LEN 20
7+
8+
enum anagram_status { UNCHECKED = -1, NOT_ANAGRAM, IS_ANAGRAM };
9+
10+
struct candidate {
11+
enum anagram_status is_anagram;
12+
const char *word;
13+
};
14+
15+
struct candidates {
16+
struct candidate *candidate;
17+
size_t count;
18+
};
19+
20+
/**
21+
* @description - determines if any of the words in candidate are anagrams
22+
* for subject. Contents of candidate structures may be modified.
23+
*/
24+
void find_anagrams(const char *subject, struct candidates *candidates);
25+
26+
#endif

0 commit comments

Comments
 (0)