Q:

Create a new string from a give string where a specified character have been removed except starting and ending position of the given string

0

Create a new string from a give string where a specified character have been removed except starting and ending position of the given string

Sample Output:

xHix
abxdddca
xajhtrb

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 str1, string c)
        {
              for (int i = str1.length() - 2; i > 0; i--)
            {
                if (str1[i] == c[0])
                {
                    str1 = str1.erase(i, 1);
                }
            }

            return str1;
        }
        
int main() 
 {
  cout << test("xxHxix", "x") << endl; 
  cout << test("abxdddca", "a") << endl; 
  cout << test("xabjbhtrb", "b") << 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