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

#include <algorithm>
#include <climits>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define EPS 1e-9

void parseUL(const string &s, vector<string> &v) {
  string ts = s;
  int i;
  for (i = 0; i < ts.size();) {
    string tmp;
    ts[i] = tolower(ts[i]);
    for (; i < ts.size() && islower(ts[i]); i++)
      tmp += ts[i];
    v.push_back(tmp);
  }
}
void parseD(const string &s, vector<string> &v) {
  int i;
  for (i = 0; i < s.size(); i++) {
    string tmp;
    for (; i < s.size() && s[i] != '_'; i++)
      tmp += s[i];
    v.push_back(tmp);
  }
}

string ext(vector<string> &v, const string &type) {
  string ret;
  if (type == "L") {
    rep(i, v.size()) {
      if (i != 0)
        v[i][0] = toupper(v[i][0]);
      ret += v[i];
    }
  } else if (type == "U") {
    rep(i, v.size()) {
      v[i][0] = toupper(v[i][0]);
      ret += v[i];
    }
  } else {
    rep(i, v.size()) {
      if (i != 0)
        ret += "_";
      ret += v[i];
    }
  }
  return ret;
}

int main() {
  string s, type;
  while (cin >> s >> type, type != "X") {
    vector<string> v;
    if (s.find('_') != string::npos) {
      parseD(s, v);
    } else {
      parseUL(s, v);
    }

    cout << ext(v, type) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define EPS 1e-9

char type_guess(string str) {
  if ('A' <= str[0] && str[0] <= 'Z')
    return 'U';
  rep(i, str.size()) if (str[i] == '_') return 'D';
  return 'L';
}
string ex(string str, char type) {
  string ret;
  char c = type_guess(str);

  if (c == type)
    return str;
  if (type == 'D' && str.size() == 1)
    exit(1);

  // cout << "c:" << c << endl;
  if (c == 'U') {
    int p = 0;
    bool flag = false;
    rep(i, str.size()) if ('A' <= str[i] && str[i] <= 'Z') {
      if (type == 'L') {
        if (i == 0)
          str[i] ^= 32;
        return str;
      } else {
        str[i] ^= 32;
        if (flag) {
          if (ret == "") {
            ret = str.substr(p, i - p);
          } else {
            ret = ret + "_" + str.substr(p, i - p);
          }
          p = i;
        }
        flag = true;
      }
    }
    return ret + "_" + str.substr(p);

  } else if (c == 'L') {
    int p = 0;
    if (type == 'U') {
      str[0] ^= 32;
      return str;
    }
    rep(i, str.size()) if ('A' <= str[i] && str[i] <= 'Z') {
      if (ret == "") {
        ret = str.substr(p, i - p);
      } else {
        ret = ret + "_" + str.substr(p, i - p);
      }
      p = i;
      // printf("p:%d\\n",p);
    }
    return ret + "_" + str.substr(p);

  } else {
    int p = 0;
    for (int i = 0; i < str.size(); i++)
      if (str[i] == '_') {
        ret += str.substr(p, i - p);
        p = i + 1;
        if (p < str.size())
          str[p] ^= 32;
        // printf("p:%d, i:%d\\n",p,i);
      }
    if (type == 'U')
      ret[0] ^= 32;
    return ret + str.substr(p);
  }
  return ret;
}
int main() {
  string str;
  char T;
  while (cin >> str >> T, T != 'X') {
    cout << ex(str, T) << endl;
  }
  return 0;
}