forked from portfoliocourses/c-example-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
anagram_checker.c
65 lines (56 loc) · 1.68 KB
/
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
53
54
55
56
57
58
59
60
61
62
63
64
65
/*******************************************************************************
*
* Program: Anagram Checker demonstration
*
* Description: Example of checking if two words are anagrams using C.
*
* YouTube Lesson: https://www.youtube.com/watch?v=R0qIYWo8igs
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
bool check_anagram(char *w1, char *w2);
// Anagram example
// Listen = Silent
int main()
{
// test the function with two words that are anagrams
char w1[] = "Listen";
char w2[] = "Silent";
if (check_anagram(w1, w2))
printf("Anagrams!\n");
else
printf("Not anagrams!\n");
return 0;
}
// returns true if words w1, w2 are anagrams, and false otherwise
// See: https://en.wikipedia.org/wiki/Anagram
bool check_anagram(char *w1, char *w2)
{
int len1 = strlen(w1);
int len2 = strlen(w2);
// keep a count of the number of occurrences of each letter from a-z in w1, w2
int w1lc[26] = {0};
int w2lc[26] = {0};
// count the number of occurrences of a-z in w1 (case insensitive)
for (int i = 0; i < len1; i++)
{
int lower = tolower(w1[i]);
w1lc[ lower - 'a' ]++;
}
// count the number of occurrences of a-z in w2 (case insensitive)
for (int i = 0; i < len2; i++)
{
int lower = tolower(w2[i]);
w2lc[ lower - 'a' ]++;
}
// if the number of occurrences of a letter is different, they're not anagrams
for (int i = 0; i < 26; i++)
if (w1lc[i] != w2lc[i]) return false;
// if the number of occurrences of a letter are all the same, they're anagrams
return true;
}