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 <algorithm>
#include <iostream>
#define between(a,x,b) ((a)<=(x)&&(x)<=(b))
using namespace std;

int main(){
  int i=0;
  string s,t;
  for(;cin >> s >> t,t!="X";){
  if(find(s.begin(),s.end(),'_')!=s.end()){
    if(t!="D"){
      for(i=0;i<s.length();i++)
        if(s[i]=='_'){
          if(s[i+1]=='_'||s[i+1]==0){cout << "Error!" << endl;return 0;}
          s.erase(i,1),s[i]-=0x20;
        }
      if(t=="U")s[0]-=0x20;
    }
    cout << s << endl;
  }else{
    if(between('A',s[0],'Z'))s[0]+=0x20;
    if(t=="U")s[0]-=0x20;
    if(t=="D")
      for(i=0;i<s.length();i++)
        if(between('A',s[i],'Z'))s.insert(i,1,'_'),s[++i]+=0x20;
    cout << s << endl;
  }
  }
}

C++: #include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
char change1(char a){
  if('A'<=a && a<='Z')
    return a;
  else
    return a+'A'-'a';
}
char change2(char a){
  if('a'<=a && a<='z')
    return a;
  else
    return a+'a'-'A';
}
int main(){
  while(1){
    string name,type;
    vector<string> word;
    cin >> name >> type;
    if(type=="X") break;
    int st = 0;
    name += "_";
    for(int i=1;i<(int)name.size();i++){
      if(name[i]=='_'){
	word.push_back(name.substr(st,i-st));
	st = i+1;
      } else if('A'<=name[i] && name[i]<='Z'){
	word.push_back(name.substr(st,i-st));
	st = i;
      }
    }
    if(type=="L"){
      for(int i=0;i<(int)word.size();i++){
	if(i==0) word[i][0]=change2(word[i][0]);
	else word[i][0]=change1(word[i][0]);
	cout << word[i];
      }
    } else if(type=="U"){
      for(int i=0;i<(int)word.size();i++){
	word[i][0]=change1(word[i][0]);
	cout << word[i];
      }
    } else {
      for(int i=0;i<(int)word.size();i++){
	word[i][0]=change2(word[i][0]);
	cout << word[i];
	if(i!=(int)word.size()-1) cout << "_";
      }
    }
    cout << endl;
  }
}

C++: #include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;

void func(string&s) {
	int d = 0;
	while ((d = (int)s.find("_", d)) >= 0)s[++d] = toupper(s[d]);
	s.erase(remove(s.begin(), s.end(), '_'), s.end());
}
int main() {
	string s, m;
	while (cin >> s >> m, m != "X") {
		if (m == "U") {
			func(s);
			s[0] = toupper(s[0]);
		}
		if (m == "L") {
			func(s);
			s[0] = tolower(s[0]);
		}
		if (m == "D") {
			s[0] = tolower(s[0]);
			rep(i, s.size()) {
				if (isupper(s[i])) {
					s.insert(i, "_"); i++;
				}
			}
			transform(s.begin(), s.end(), s.begin(), ::tolower);
		}
		cout << s << endl;
	}
}

C++: #include <iostream>
#include <string>
#include <vector>

using namespace std;

vector<string> parse(string s){
    if('A' <= s[0] && s[0] <= 'Z') s[0] -= 'A'-'a';
    vector<string> ret;
    int t = 0;
    while(s.size()){
        if('A' <= s[t] && s[t] <= 'Z' || s[t] == '_'){
            if(s[t] == '_'){ret.push_back(s.substr(0, t)); s = s.substr(t+1);t=0;}
            else {ret.push_back(s.substr(0, t)); s = s.substr(t); s[0] -= 'A'-'a'; t=0;}
        }
        else if(t==s.size()){ret.push_back(s); break;}
        else t++;
    }
    return ret;
}

int main(){
    string s, t;
    while(cin >> s >> t, t != "X"){
        vector<string> v = parse(s);
        for(int i=0; i< v.size(); i++){
            if(t=="L"){
                if(i!=0) v[i][0]+='A'-'a';
                cout << v[i];
            }
            else if(t=="U"){
                v[i][0]+='A'-'a';
                cout << v[i];
            }
            else {
                cout << (i==0?"":"_") << v[i];
            }
        }
        cout << endl;
    }
}

C++: //37
#include<iostream>
#include<string>
#include<sstream>
#include<cctype>

using namespace std;

int main(){
  string s;
  char c;
  while(cin>>s>>c,c!='X'){
    s[0]=tolower(s[0]);
    string su;
    for(int i=0;i<s.size();i++){
      if(isupper(s[i])){
	su+='_';
      }
      su+=tolower(s[i]);
    }
    if(c=='D'){
      cout<<su<<endl;
    }else{
      string so;
      bool f=false;
      for(int i=0;i<su.size();i++){
	if(su[i]=='_'){
	  f=true;
	}else{
	  so+=(f)?toupper(su[i]):tolower(su[i]);
	  f=false;
	}
      }
      if(c=='U'){
	so[0]=toupper(so[0]);
      }
      cout<<so<<endl;
    }
  }
  return 0;
}