-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringReversal.php
More file actions
66 lines (57 loc) · 1.77 KB
/
StringReversal.php
File metadata and controls
66 lines (57 loc) · 1.77 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
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
<?php
/**
* String Reversal Problems
*
* We will reverse strings
*
* @category InterviewQuestions
* @package ReverseStrings
* @author Michael Reeves <mike.reeves@gmail.com>
* @date 2013-05-10
*
*/
// step 1 -> write a function to reverse this, return a string
function reverseString($input){
$inputArray = str_split($input);
$output = '';
$length = count($inputArray);
for ($i = $length - 1; $i > -1; $i--) {
$output .= $inputArray[$i];
}
return $output;
}
$input = "This is a test";
echo '<b>Test reverseString function:</b><br/>';
echo reverseString($input).'<br/><br/>'; // should be: "tset a si sihT"
// step 2 -> same reversal but preserve words
function reverseWords($input){
$inputArray = explode(" ", $input);
$output = '';
$length = count($inputArray);
for ($i = $length - 1; $i > -1; $i--) {
$outputArray[] = $inputArray[$i];
}
$output = implode(' ', $outputArray);
return $output;
}
$input = "This is a test";
echo '<b>Test reverseWords function:</b><br/>';
echo reverseWords($input).'<br/><br/>'; // should be: "test a is This"
// step 3 - modify reverseString and use recursion
function reverseRecurseWrapper($input) {
$inputArray = str_split($input);
$outputArray = recursePopPush($inputArray);
$output = implode("", $outputArray);
return $output;
}
function recursePopPush($inputArray, $outputArray = array()){
if (count($inputArray) > 0) {
$letter = array_pop($inputArray);
$outputArray[] = $letter;
$outputArray = recursePopPush($inputArray, $outputArray);
}
return $outputArray;
}
$input = "This is a test";
echo '<b>Test reverseRecurseWrapper function:</b><br/>';
echo reverseRecurseWrapper($input).'<br/><br/>'; // should be: "tset a si sihT"