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

#include <iostream>
#include <string>
using namespace std;

string V, W;
int n;

string J(string S) {
  string T = "";
  T = S[S.size() - 1] + S.substr(0, S.size() - 1);
  return T;
}

string C(string S) {
  string T = "";
  T = S.substr(1, S.size() - 1) + S[0];
  return T;
}

string E(string S) {
  string T = "";
  T += S.substr(S.size() - (S.size() / 2), S.size() / 2);
  if (S.size() % 2 == 1) {
    T += S[S.size() / 2];
  }
  T += S.substr(0, S.size() / 2);
  return T;
}

string A(string S) {
  string T = "";
  for (int k = S.size() - 1; k >= 0; k--) {
    T += S[k];
  }
  return T;
}

string P(string S) {
  string T = "";
  for (int k = 0; k < S.size(); k++) {
    if (isdigit(S[k])) {
      T += (S[k] - 1);
      if (T[k] < '0') {
        T[k] += 10;
      }
    } else {
      T += S[k];
    }
  }
  return T;
}

string M(string S) {
  string T = "";
  for (int k = 0; k < S.size(); k++) {
    if (isdigit(S[k])) {
      T += (S[k] + 1);
      if (T[k] > '9') {
        T[k] -= 10;
      }
    } else {
      T += S[k];
    }
  }
  return T;
}

string message(string S, string T) {
  for (int j = S.size() - 1; j >= 0; j--) {
    if (S[j] == 'J') {
      T = J(T);
    }
    if (S[j] == 'C') {
      T = C(T);
    }
    if (S[j] == 'E') {
      T = E(T);
    }
    if (S[j] == 'A') {
      T = A(T);
    }
    if (S[j] == 'P') {
      T = P(T);
    }
    if (S[j] == 'M') {
      T = M(T);
    }
  }
  return T;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> V >> W;
    cout << message(V, W) << endl;
  }
}

Error: -6
*** stack smashing detected ***: terminated


Fixed submission:

#include <iostream>
#include <string>
using namespace std;

string V, W;
int n;

string J(string S) {
  string T = "";
  T = S[S.size() - 1] + S.substr(0, S.size() - 1);
  return T;
}

string C(string S) {
  string T = "";
  T = S.substr(1, S.size() - 1) + S[0];
  return T;
}

string E(string S) {
  string T = "";
  T += S.substr(S.size() - (S.size() / 2), S.size() / 2);
  if (S.size() % 2 == 1) {
    S += S[S.size() / 2];
  }
  T += S.substr(0, S.size() / 2);
  return T;
}

string A(string S) {
  string T = "";
  for (int k = S.size() - 1; k >= 0; k--) {
    T += S[k];
  }
  return T;
}

string P(string S) {
  string T = "";
  for (int k = 0; k < S.size(); k++) {
    if (isdigit(S[k]) == true) {
      T += (S[k] - 1);
      if (T[k] < '0') {
        T[k] += 10;
      }
    } else {
      T[k] += S[k];
    }
  }
  return T;
}

string M(string S) {
  string T = "";
  for (int k = 0; k < S.size(); k++) {
    if (isdigit(S[k]) == true) {
      T += (S[k] + 1);
      if (T[k] > '9') {
        T[k] -= 10;
      }
    } else {
      T[k] += S[k];
    }
  }
  return T;
}

string message(string S, string T) {
  for (int j = S.size() - 1; j >= 0; j--) {
    if (S[j] == 'J') {
      T = J(T);
    }
    if (S[j] == 'C') {
      T = C(T);
    }
    if (S[j] == 'E') {
      T = E(T);
    }
    if (S[j] == 'A') {
      T = A(T);
    }
    if (S[j] == 'P') {
      T = P(T);
    }
    if (S[j] == 'M') {
      T = M(T);
    }
  }
  return T;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> V >> W;
    cout << message(V, W) << endl;
  }
}