Write a C++ program to create a new string from a given string without the first and last character if the first or last characters are 'a' otherwise return the original given string

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

Write a C++ program to create a new string from a given string without the first and last character if the first or last characters are 'a' otherwise return the original given string

Sample Output:

bcab
python
bcd
jython

الأجوبة

#include <iostream>
using namespace std;

string test(string s1)
          {
          if (s1.length() > 0 && s1.substr(s1.length() - 1) == "a")
            {
                s1 = s1.erase(s1.length() - 1);
            }

            if (s1.length() > 0 && s1.substr(0, 1) == "a")
            {
                s1 = s1.erase(0, 1);
            }

            return s1;
          }              
        
int main() 
 {
  cout << test("abcab") << endl;  
  cout << test("python") << endl; 
  cout << test("abcda") << endl;  
  cout << test("jython") << endl;  
  return 0;    
}
هل كان المحتوى مفيد؟

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

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