-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnagrams.php
More file actions
37 lines (34 loc) · 1.15 KB
/
Anagrams.php
File metadata and controls
37 lines (34 loc) · 1.15 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
<?php
// For each pair of words in the array return true or false
// if the 2 words are anagrams of each other
$words = [['dusty','study'],['break','beaks'],['inch','chin'],['elbow','below'],['kings','sings']];
// A little function, seems to be the best way to check.
function check_anagram($word1, $word2) {
// count_chars checks the characters. Compare to make sure they are the same. TRUE.
if (count_chars($word1) == count_chars($word2)) {
echo '<b>True</b>, ';
} else {
// Else False.
echo '<i>False</i>, ';
}
}
// Foreach the outter array,
foreach($words as $wordArray) {
// vars for $word1 and $word2 storage. Wiped when next check is done.
$word1 = '';
$word2 = '';
// inner array for the two words to be compared.
foreach($wordArray as $key => $word) {
// swtich using $key to identify $word1 and $word2.
switch ($word) {
case $key == 0:
$word1 = $word;
break;
case $key == 1:
$word2 = $word;
break;
}
}
// call to check_anagram outside inner foreach.
check_anagram($word1, $word2);
}