Problem p00819 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

string turn1(string S) { return S.substr(1, S.size() - 1) + S[0]; }

string turn2(string S) { return S[S.size() - 1] + S.substr(0, S.size() - 1); }

string turn3(string S) {
  string T;

  if (S.size() % 2 == 1) {
    T = S.substr(S.size() / 2 + 1, S.size() / 2) + S[S.size() / 2] +
        S.substr(0, S.size() / 2);
  } else {
    T = S.substr(S.size() / 2, S.size() / 2) + S.substr(0, S.size() / 2);
  }

  return T;
}

string turn4(string S) {
  reverse(S.begin(), S.end());

  return S;
}

string turn5(string S) {
  for (int i = 0; i < S.size(); i++) {
    if (isdigit(S[i])) {
      if (S[i] == '9') {
        S[i] = '0';
      } else {
        S[i]++;
      }
    }
  }

  return S;
}

string turn6(string S) {
  for (int i = 0; i < S.size(); i++) {
    if (isdigit(S[i])) {
      if (S[i] == '0') {
        S[i] = '9';
      } else {
        S[i]--;
      }
    }
  }

  return S;
}

int main() {
  int n;
  string S, T;

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> S >> T;

    for (int i = S.size() - 1; i >= 0; i--) {
      switch (S[i]) {
      case 'C':
        T = turn1(T);
        break;
      case 'J':
        T = turn2(T);
        break;
      case 'E':
        T = turn3(T);
        break;
      case 'A':
        T = turn4(T);
        break;
      case 'M':
        T = turn5(T);
        break;
      case 'P':
        T = turn6(T);
        break;
      }
    }

    cout << T << endl;
  }

  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

string turn1(string S) { return S.substr(1, S.size() - 1) + S[0]; }

string turn2(string S) { return S[S.size() - 1] + S.substr(0, S.size() - 1); }

string turn3(string S) {
  string T;

  if (S.size() % 2 == 1) {
    T = S.substr(S.size() / 2 + 1, S.size() / 2) + S[S.size() / 2] +
        S.substr(0, S.size() / 2);
  } else {
    T = S.substr(S.size() / 2, S.size() / 2) + S.substr(0, S.size() / 2);
  }

  return T;
}

string turn4(string S) {
  reverse(S.begin(), S.end());

  return S;
}

string turn5(string S) {
  for (int i = 0; i < S.size(); i++) {
    if (isdigit(S[i])) {
      if (S[i] == '9') {
        S[i] = '0';
      } else {
        S[i]++;
      }
    }
  }
}

string turn6(string S) {
  for (int i = 0; i < S.size(); i++) {
    if (isdigit(S[i])) {
      if (S[i] == '0') {
        S[i] = '9';
      } else {
        S[i]--;
      }
    }
  }
}

int main() {
  int n;
  string S, T;

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> S >> T;

    for (int i = S.size() - 1; i >= 0; i--) {
      switch (S[i]) {
      case 'C':
        T = turn1(T);
        break;
      case 'J':
        T = turn2(T);
        break;
      case 'E':
        T = turn3(T);
        break;
      case 'A':
        T = turn4(T);
        break;
      case 'M':
        T = turn5(T);
        break;
      case 'P':
        T = turn6(T);
        break;
      }
    }

    cout << T << endl;
  }

  return 0;
}