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 <iostream>
#include <string>
#include <vector>
#include <cctype>
#include <cstring>
using namespace std;

vector<string> split(string s) {
    vector<string> ret;
    string buf;
    if (isupper(s[0])) {
        s[0] = tolower(s[0]);
    }
    for (int i = 0; i < s.size(); i++) {
        if (s[i] == '_') {
            ret.push_back(buf);
            buf.clear();
        } else if (isupper(s[i])) {
            ret.push_back(buf);
            buf.clear();
            buf.push_back(tolower(s[i]));
        } else {
            buf.push_back(s[i]);
        }
    }
    ret.push_back(buf);
    return ret;
}

int main() {
    string s; char type;
    while (cin >> s >> type) {
        if (type == 'X') break;
        vector<string> words = split(s);

        string output;
        switch (type) {
            case 'L':
                for (int i = 1; i < words.size(); i++) {
                    words[i][0] = toupper(words[i][0]);
                }
                for (int i = 0; i < words.size(); i++) {
                    output += words[i];
                }
                break;
            case 'U':
                for (int i = 0; i < words.size(); i++) {
                    words[i][0] = toupper(words[i][0]);
                    output += words[i];
                }
                break;
            case 'D':
                for (int i = 0; i < words.size()-1; i++) {
                    words[i].push_back('_');
                }
                for (int i = 0; i < words.size(); i++) {
                    output += words[i];
                }
                break;
        }
        cout << output << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <set>
#include <vector>
#include <cstring>
#include <climits>
#include <queue>
#include <map>
#include <sstream>
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;
}

C++: #include<iostream>
#include<string>
#include<cctype>
#include<algorithm>

using namespace std;

void UpperCamelCase(string&);
void LowerCamelCase(string&);
void UnderScoreCase(string&);
bool IsUpperCase(char);

int main(){
  char c;
  string str;

  while(1){
    cin >> str >> c;

    if(c == 'X') break;
    else if(c == 'U') UpperCamelCase(str);
    else if(c == 'L') LowerCamelCase(str);
    else if(c == 'D') UnderScoreCase(str);

    cout << str << endl;
  }

  return 0;
}

void UpperCamelCase(string& s){
  string::iterator i;

  if(s[0] >= 'a' && s[0] <= 'z')
    s[0] = toupper(s[0]);

  i = s.begin();
  while(1){
    i = find(i, s.end(), '_');
    if(i == s.end()) break;
    i = s.erase(i);
    *i = toupper(*i);
  }
}

void LowerCamelCase(string& s){
  string::iterator i;

  if(s[0] >= 'A' && s[0] <= 'Z')
    s[0] = tolower(s[0]);

  i = s.begin();
  while(1){
    i = find(i, s.end(), '_');
    if(i == s.end()) break;
    i = s.erase(i);
    *i = toupper(*i);
  }
}

void UnderScoreCase(string& s){
  string::iterator i;

  if(s[0] >= 'A' && s[0] <= 'Z')
    s[0] = tolower(s[0]);

  i = s.begin();
  while(1){
    i = find_if(i, s.end(), IsUpperCase);
    if(i == s.end()) break;
    *i = tolower(*i);
    i = s.insert(i, '_');
    ++i;
  }
}

bool IsUpperCase(char c){
  if(c >= 'A' && c <= 'Z')
    return true;
  else
    return false;
}