Q:

Write a C++ program to create a new string from a given string. If the two characters of the given string from its beginning and end are same return the given string without the first two characters otherwise return the original string

0

Write a C++ program to create a new string from a given string. If the two characters of the given string from its beginning and end are same return the given string without the first two characters otherwise return the original string

Sample Output:

cab
Python
cabab

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <iostream>
using namespace std;

string test(string s1)
          {
            if (s1.length() > 1 && s1.substr(0, 2) == s1.substr(s1.length() - 2))
            {
                return s1.substr(2);
            }
            else
            {
                return s1;
            }
        }
        
int main() 
 {
  cout << test("abcab") << endl;  
  cout << test("Python") << endl; 
  cout << test("abcabab") << endl;  
  return 0;    
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now