Write a PHP program to create a new string from a given string after swapping last two characters
<?php function test($s1) { if (strlen($s1) > 1) { return substr($s1, 0, strlen($s1) - 2) . substr($s1, strlen($s1)-1, 1) . substr($s1, strlen($s1)-2, 1); } else { return $s1; } } echo test("Hello")."\n"; echo test("Python")."\n"; echo test("PHP")."\n"; echo test("JS")."\n"; echo test("C")."\n";
Sample Output:
Helol Pythno PPH SJ C
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Sample Output:
need an explanation for this answer? contact us directly to get an explanation for this answer