I am trying to write a function that will take lets say "hey there" then create an array of all prefix of this string. So it would return "h", "he", "hey", "hey ", "hey t" etc etc.
Then I would like the same function to create a second array of all suffixs (counting backwards down the string). so for the same string it would return "e", "er", "ere", "ereh", "ereht", "ereht " etc
I am sturggling a bit to figure this one out but I have managed to get the following which gets all possible combinations of a string I just need to get it to do it only in order.
$str = "hey there";
function permute($str,$i,$n) {
if ($i == $n)
print "$str\n";
else {
for ($j = $i; $j < $n; $j++) {
swap($str,$i,$j);
permute($str, $i+1, $n);
swap($str,$i,$j); // backtrack.
}
}
}
// function to swap the char at pos $i and $j of $str.
function swap(&$str,$i,$j) {
$temp = $str[$i];
$str[$i] = $str[$j];
$str[$j] = $temp;
}
permute($str,0,strlen($str)); // call the function.
}
Any help greatly appreciated.