Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func configure(scanner *bufio.Scanner) {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
}
func getNextString(scanner *bufio.Scanner) string {
	scanned := scanner.Scan()
	if !scanned {
		panic("scan failed")
	}
	return scanner.Text()
}
func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}
func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}
func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	extra := 0
	if os.Getenv("I") == "IronMan" {
		fp, _ = os.Open(os.Getenv("END_GAME"))
		extra = 100
	}
	scanner := bufio.NewScanner(fp)
	configure(scanner)
	writer := bufio.NewWriter(wfp)
	defer func() {
		r := recover()
		if r != nil {
			fmt.Fprintln(writer, r)
		}
		writer.Flush()
	}()
	solve(scanner, writer)
	for i := 0; i < extra; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	for s, c := getNextString(scanner), getNextString(scanner); c != "X"; s, c = getNextString(scanner), getNextString(scanner) {
		if strings.Index(s, "_") != -1 {
			fmt.Fprintln(writer, translate(strings.Split(s, "_"), c))
			continue
		}
		ss := make([]string, 0)
		for len(s) > 0 {
			index := nextupper(s[1:])
			ss = append(ss, s[:index+1])
			s = s[index+1:]
		}
		fmt.Fprintln(writer, translate(ss, c))
	}
}
func translate(ss []string, c string) string {
	switch c {
	case "U":
		return u(ss)
	case "L":
		return l(ss)
	case "D":
		return d(ss)
	}
	return ""
}

func nextupper(s string) int {
	n := len(s)
	for i := 0; i < n; i++ {
		if s[i:i+1] == strings.ToUpper(s[i:i+1]) {
			return i
		}
	}
	return n
}
func u(ss []string) string {
	n := len(ss)
	for i := 0; i < n; i++ {
		ss[i] = fmt.Sprintf("%s%s", strings.ToUpper(ss[i][0:1]), ss[i][1:])
	}
	return strings.Join(ss, "")
}
func l(ss []string) string {
	n := len(ss)
	ss[0] = strings.ToLower(ss[0])
	for i := 1; i < n; i++ {
		ss[i] = fmt.Sprintf("%s%s", strings.ToUpper(ss[i][0:1]), ss[i][1:])
	}
	return strings.Join(ss, "")
}
func d(ss []string) string {
	n := len(ss)
	for i := 0; i < n; i++ {
		ss[i] = strings.ToLower(ss[i])
	}
	return strings.Join(ss, "_")
}

C++: #include <cstdio>
#include <cstring>
#include <cctype>

int main(){
	while(true){
		char str[101], cmd[2];
		scanf("%100s %1s", str, cmd);
		if(cmd[0] == 'X'){ break; }
		bool isFirst = true, isPause = true;
		for(char *p = str; *p != '\0'; p++){
			if(!isFirst){
				if(!(*p & 0x20)){ isPause = true; }
				if(*p == '_'){
					isPause = true;
					if(*++p == '\0'){ break; }
				}
			}
			switch(cmd[0]){
			case 'U':
				if(isPause){
					putc(toupper(*p), stdout);
				}else{
					putc(tolower(*p), stdout);
				}
				break;
			case 'L':
				if(isPause && !isFirst){
					putc(toupper(*p), stdout);
				}else{
					putc(tolower(*p), stdout);
				}
				break;
			case 'D':
				if(isPause && !isFirst){
					putc('_', stdout); putc(tolower(*p), stdout);
				}else{
					putc(tolower(*p), stdout);
				}
			}
			isPause = false;
			isFirst = false;
		}
		puts("");
	}
	return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <cctype>
using namespace std;

#define rep(i,n) for(int (i)=0; (i)<(int)(n); ++(i))
#define foreach(c,i) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)

int N;
string in;
char type;

void solve() {
  if (type == 'X') return;
  N = in.size();
  bool is_us = false;
  vector<string> ret;
  if (in.find('_') != -1) { is_us = true; }
  if (is_us) {
    // underscore
    if (type == 'D') { printf("%s\n",in.c_str()); return; }
    int cur = 0;
    for (int i = 1; i < in.size(); ++i) {
      if (in[i] == '_') {
        ret.push_back(in.substr(cur,i-cur));
        cur = i+1;
      }
    }
    ret.push_back(in.substr(cur, in.size()-cur));
    rep(i,ret.size()) ret[i][0] = toupper(ret[i][0]);
    if (type == 'L') ret[0][0] = tolower(ret[0][0]);
    rep(i,ret.size()) printf("%s",ret[i].c_str()); puts("");
  } else {
    // L or U
    int cur = 0;
    for (int i = 1; i < in.size(); ++i) {
      if (isupper(in[i])) {
        ret.push_back(in.substr(cur, i-cur));
        cur = i;
      }
    }
    ret.push_back(in.substr(cur, in.size()-cur));
    if (type == 'D') {
      rep(i,ret.size()) ret[i][0] = tolower(ret[i][0]);
      rep(i,ret.size()) {
        if(i) putchar('_'); printf("%s",ret[i].c_str());
      } puts("");
    } else {
      if (type == 'U') ret[0][0] = toupper(ret[0][0]);
      else ret[0][0] = tolower(ret[0][0]);
      rep(i,ret.size()) printf("%s",ret[i].c_str()); puts("");
    }
  }
}

int main() {
  while (cin >> in >> type) solve();
  return 0;
}

C++: #include <iostream>
#include <cctype>
#include <string>
#include <vector>
using namespace std;

int main() {
  string s;
  while(cin >> s) {
    int p=0;
    vector<string> v;
    char c;
    cin >> c;
    if (c == 'X') break;
    
    for (int i=0; i<s.length(); i++) {
      if (s[i] == '_') {
        v.push_back(s.substr(p,i-p));
        p=i+1;
      }
      if (i>0 && isupper(s[i])) {
        v.push_back(s.substr(p,i-p));
        p=i;
      }
    }
    v.push_back(s.substr(p));
    
    string ans;
    for (int i=0; i<v.size(); i++) {
      string s1 = v[i];
      if (c == 'U') {
        s1[0] = toupper(s1[0]);
        ans += s1;
      }
      if (c == 'L') {
        if (i==0) {
          s1[0] = tolower(s1[0]);
          ans += s1;
        } else {
          s1[0] = toupper(s1[0]);
          ans += s1;
        }
      }
      
      if (c == 'D') {
        s1[0] = tolower(s1[0]);
        if (i>=1)
          ans += "_" + s1;
        else
          ans += s1;
      }
    }
    cout << ans << endl;
  }
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<cctype>
using namespace std;

vector<string> split(string s){
    vector<string> ret;
    
    string tmp = s.substr(0,1);
    tmp[0] = tolower(tmp[0]);

    for(int i = 1; i < s.length(); i++){
        if(s[i] == '_'){
            ret.push_back(tmp);
            tmp = "";
        }else if(isupper(s[i])){
            ret.push_back(tmp);
            tmp = s.substr(i, 1);
            tmp[0] = tolower(tmp[0]);
        }else{
            tmp += s.substr(i,1);
        }
    }

    if(tmp != "")   ret.push_back(tmp);
    
    return ret;
}

int main(){
    string s;
    char op;
    while(cin >> s >> op){
        if(op == 'X')   break;

        vector<string> v = split(s);
        int n = v.size();

        if(op == 'L'){
            cout << v[0];
            for(int i = 1; i < n; i++){
                v[i][0] = toupper(v[i][0]);
                cout << v[i];
            }
            cout << endl;
        }else if(op == 'U'){
            for(int i = 0; i < n; i++){
                v[i][0] = toupper(v[i][0]);
                cout << v[i];
            }
            cout << endl;
        }else if(op == 'D'){
            for(int i = 0; i < n; i++){
                cout << v[i] << "_\n"[i == n-1];
            }
        }
    }
    return 0;
}