-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
39 lines (29 loc) · 1.1 KB
/
Copy pathindex.php
File metadata and controls
39 lines (29 loc) · 1.1 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
38
39
<?php
/**
* This function checks of the given sentence is a pangram.
*/
function check(string $sentence): string
{
// Create an array of the alphabet
$alphabet = range('a', 'z');
$lowercase = strtolower($sentence);
$trimmed = str_replace(' ', '', $lowercase);
// Create an array of the sentence's characters
$characters = str_split($trimmed);
$matches = 0;
// Loop through the sentence's characters and remove any matches from the alphabet array
foreach ($characters as $character) {
if (in_array($character, $alphabet)) {
$alphabet = array_diff($alphabet, [$character]);
$matches++;
}
}
// If the alphabet array is empty, the sentence is a pangram
$result = $matches === 26 ? 'is' : 'is not';
return "\nThe sentence '$sentence' $result a pangram." . PHP_EOL;
}
$pangram1 = 'The quick brown fox jumps over the lazy dog.';
$pangram2 = 'The five boxing wizards jump quickly.';
$pangram3 = 'John quickly extemporized five tow bags.';
$normal = 'This is not a pangram by any stretch of the imagination.';
echo check($normal);