PHP Exercises: Move the last two characters to the start of a given string of length at least two
PHP Basic Algorithm: Exercise-68 with Solution
Write a PHP program to move the last two characters to the start of a given string of length at least two.
Sample Solution:
PHP Code :
<?php
// Define a function named 'test' that rearranges the last two characters of a string with the rest of the string
function test($s1)
{
// Use substr to extract substrings and rearrange them
return substr($s1, strlen($s1) - 2, 2) . substr($s1, 0, strlen($s1) - 2);
}
// Test the 'test' function with different strings and display the results
echo test("Hello")."\n";
echo test("JS")."\n";
?>
Sample Output:
loHel JS
Flowchart:
PHP Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a PHP program to move the first two characters to the end of a given string of length at least two.
Next: Write a PHP program to create a new string without the first and last character of a given string of any length.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics