w3resource

PHP Exercises: Move the first two characters to the end of a given string of length at least two

PHP Basic Algorithm: Exercise-67 with Solution

Write a PHP program to move the first two characters to the end of a given string of length at least two.

Sample Solution:

PHP Code :

<?php
// Define a function named 'test' that rearranges the characters of a string
function test($s1)
{ 
    // Use substr to extract substrings and rearrange them
    return substr($s1, 2, strlen($s1) - 2) . substr($s1, 0, 2);
}

// Test the 'test' function with different strings and display the results
echo test("Hello")."\n";
echo test("JS");
?>

Sample Output:

lloHe
JS

Flowchart:

Flowchart: Move the first two characters to the end of a given string of length at least two.

PHP Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a PHP program to concat two given string of length atleast 1, after removing their first character.
Next: Write a PHP program to move the last two characters to the start of a given string of length at least two.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.