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

  • برمجة سي بلس بلس
  • برمجة

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

Sample Output:

Helol
Pythno
PPH
SJ
C

الأجوبة

#include <iostream>
using namespace std;

string test(string s1)
        {
          if (s1.length() > 1)
            {
                return s1.substr(0, s1.length() - 2) + s1[s1.length() - 1] + s1[s1.length() - 2];
            }
            else
            {
                return s1;
            }
        }
        
int main() 
 {
  cout << test("Hello") << endl;  
  cout << test("Python") << endl; 
  cout << test("PHP") << endl;  
  cout << test("JS") << endl;  
  cout << test("C") << endl;  
  return 0;    
}
هل كان المحتوى مفيد؟

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

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