-
Notifications
You must be signed in to change notification settings - Fork 0
/
casey-test.php
55 lines (38 loc) · 1.15 KB
/
casey-test.php
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
<?php
/*
We need a method that accepts a string as input, and returns it in title case as the output.
What is Title Case?
Title case means that the first letter of each word is capitalized.
Examples:
Input: "THE LAZY DOG WALKS Home."
Ouput: "The Lazy Dog Walks Home."
Input: "the lazy dog walks 1 block home."
Ouput: "The Lazy Dog Walks 1 Block Home."
Input: "a LazY dOG waLkS homE."
Ouput: "A Lazy Dog Walks Home."
*/
function wordCapitalize(string $str) {
$resp = '';
for($i = 0; $i < strlen($str); $i++) {
$resp .= (isset($str[$i - 1]) && $str[$i - 1] === ' ') || $i === 0 ? strtoupper($str[$i]) : strtolower($str[$i]);
}
return $resp;
}
function wordCapitalizeViaExplode(string $str) {
$strArr = explode(" ", $str);
foreach ($strArr as &$word) {
$word = ucfirst(strtolower($word));
}
return implode(" ", $strArr);
}
// test cases
$testArr = [
"THE LAZY DOG WALKS Home.",
"the lazy dog walks 1 block home.",
"a LazY dOG waLkS homE.",
];
foreach ($testArr as $val) {
echo wordCapitalize($val) . PHP_EOL;
echo wordCapitalizeViaExplode($val) . PHP_EOL;
echo PHP_EOL;
}