logo CodeStepByStep logo

swap_pairs

Language/Type: PHP string return

Write a function named swap_pairs that accepts a string as a parameter and returns a new string such that each pair of adjacent letters will be reversed. If the string has an odd number of letters, the last letter is unchanged. For example, if a string variable s stores "example", the call of swap_pairs(s); should return "xemalpe". If s had been "hello there", the call would produce "ehll ohtree".

Function: Write a PHP function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.