Write a PHP program to create a new string from a given string after swapping last two characters

  • برمجة بي اتش بي

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
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...