-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathch9_prog_proj_04.c
71 lines (56 loc) · 1.01 KB
/
ch9_prog_proj_04.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
66
67
68
69
70
71
/*
* ch9_prog_proj_04.c
*
* Created on: Oct 25, 2019
* Author: SuperMoudy
*/
// Programming Project 4: Anagrams
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
void read_word(int counts[26]);
bool equal_array(int counts1[26], int counts2[26]);
int main(void)
{
// Arrays to detect anograms
int word1_count[26] = {0}, word2_count[26] = {0};
// Reading the first word
printf("Enter first word: ");
read_word(word1_count);
// Reading the second word
printf("Enter second word: ");
read_word(word2_count);
// Anagrams test
if(equal_array(word1_count, word2_count))
{
printf("The words are anagrams");
}
else
{
printf("The words are not anagrams");
}
return 0;
}
void read_word(int counts[26])
{
// Input char
char c;
do
{
c = getchar();
if(isalpha(c))
{
c = tolower(c);
counts[c - 'a']++;
}
}while(c != '\n');
}
bool equal_array(int counts1[26], int counts2[26])
{
for(int i = 0; i < 26; i++)
{
if(counts1[i] != counts2[i])
return false;
}
return true;
}