-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstring_transformer.php
More file actions
24 lines (21 loc) · 939 Bytes
/
string_transformer.php
File metadata and controls
24 lines (21 loc) · 939 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
// 6 kyu - String transformer
// Given a string, return a new string that has transformed based on the input:
//
// Change case of every character, ie. lower case to upper case, upper case to lower case.
// Reverse the order of words from the input.
// For example:
// stringTransformer('Example Input')/string_transformer("Example Input") (depending on the language you are completing the Kata in) should return 'iNPUT eXAMPLE'
//
// You may assume the input only contain English alphabet and spaces.
function string_transformer(string $s): string {
$result = array_map(function($l) {
return ctype_upper($l) ? strtolower($l) : strtoupper($l);
}, str_split($s));
return implode(' ', array_reverse(explode(' ', implode($result))));
}
// Alternative Solution:
// function string_transformer(string $str): string {
// return implode(' ',array_reverse(explode(' ',strtolower($str) ^ strtoupper($str) ^ $str)));
// }
?>