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>
using namespace std;
int main(){
  int i,len;
  char type;
  string name,out;
  while(1){
    out="";
    cin >> name >> type;
    if(type=='X') break;
    len=name.size();
    for(i=0;i<len;i++){
      if(i==0){
	if(name[i]<'a'&&type!='U') out+=name[i]-'A'+'a';
	else if(name[i]>='a'&&type=='U') out+=name[i]-'a'+'A';
	else out+=name[i];
      }
      else{
	if(name[i]=='_'&&type!='D'){
	  out+=name[i+1]-'a'+'A';
	  i++;
	}
	else if(name[i]<'a'&&type=='D'){
	  out+='_';
	  out+=name[i]-'A'+'a';
	}else out+=name[i];
      }
    }
    cout << out << endl;
  }
  return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <stack>
#include <queue>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
static const double EPS = 1e-5;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)

int main(void){
  string name,type;
  while(cin>>name>>type){
    if(type=="X") break;
    if(type=="D"){
      FOR(i,1,name.size()){
        if(isupper(name[i])){
          name.insert(i++,"_");
        }
      }
      REP(i,name.size()) 
          name[i] = tolower(name[i]);
      cout<<name<<endl;
    }else{
      REP(i,name.size()){
        if(name[i]=='_'){
          name[i+1] = toupper(name[i+1]);
          name.erase(i,1);
        }
      }
      if(type=="U"){
        name[0] = toupper(name[0]);
        cout<<name<<endl;
      }else{
        name[0] = tolower(name[0]);
        cout<<name<<endl;
      }
    }
  }
  return 0;
}

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

string s ;
char ans[200] ;

int main(){
	
	char t ;
	
	while( cin >> s >> t ){
		if( t == 'X' ) break ;
		
		int p = 1 ;
		
		if( s[0] <= 90 && t != 'U' ) ans[0] = s[0] +32 ;
		else if( s[0] >= 97 && t == 'U' ) ans[0] = s[0] -32 ;
		else ans[0] = s[0] ;
		for( int i=1 ; i<s.size() ; i++ ){
			if( s[i] == '_' && t != 'D' ){
				ans[p] = s[i+1] -32 ;
				i++ ;
			}
			else if( s[i] <= 90 && t == 'D' ){
				ans[p] = '_' ;
				ans[p+1] = s[i] +32 ;
				p++ ;
			} else ans[p] = s[i] ;
			
			p++ ;
		}
		
		for( int i=0 ; i<p ; i++ ){
			cout << ans[i] ;
		}cout << endl ;
	}
	
return 0 ;
}

C++: #include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
 
using namespace std;
 
#define rep(i,n) for(int i=0; i<(n); i++)
#define repc(i,s,e) for(int i=(s); i<(e); i++)
#define pb(n) push_back((n))
#define mp(n,m) make_pair((n),(m))
#define all(r) r.begin(),r.end()
#define fi first
#define se second
 
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
 
const int INF = 10000000;

vector<string> split(const string &s, char c = ' '){
	vector<string> ret;
	stringstream ss(s);
	string t;
	while(getline(ss,t,c)){
		ret.pb(t);
	}
	return ret;
}

vector<string> get_word(string s){
	vector<string> ret;
	for(int i = 0; i < s.size(); i++){
		for(int j = i; j < s.size(); j++){
			if((s[j] >= 'A' && s[j] <= 'Z') || j == s.size() - 1){
				ret.push_back(s.substr(i, j - i));
			}
		}
	}
	return ret;
}

string calc(string s, string t1, string t2){
	string ret;
	if(t1 == "U"){
		if(t2 == "L"){
			ret = s;
			ret[0] += 'a' - 'A';
		}
		else {
			s[0] += 'a' - 'A';
			vector<string> v = get_word(s);
			for(int i = 0; i < v.size(); i++){
				s = v[i];
				s[0] +='a' - 'A';
				if(ret.size() != 0) ret += "_";
				ret += s; 
			}
		}
	}
	else if(t1 == "L"){
		if(t2 == "U"){
			ret = s;
			ret[0] += 'A' - 'a';
		}
		else{
			vector<string> v = get_word(s);
			for(int i = 0; i < v.size(); i++){
				s = v[i];
				s[0] +='a' - 'A';
				if(ret.size() != 0) ret += "_";
				ret += s; 
			}
		}
	}
	else{
		if(t2 == "U"){
			vector<string> v = split(s, '_');
			for(int i = 0; i < v.size(); i++){
				s = v[i];
				s[0] += 'A' - 'a';
				ret += s;
			}
		}
		else{
			vector<string> v = split(s, '_');
			for(int i = 0; i < v.size(); i++){
				s = v[i];
				if(i != 0) s[0] += 'A' - 'a';
				ret += s;
			}
		}
	}
	return ret;
}

int main(){
	string s, t;
	while(cin>>s>>t && t != "X"){
		if(s.find("_") != string::npos) cout<<calc(s, "D", t);
		else if(s[0] >= 'A' && s[0] <= 'Z') cout<<calc(s, "U", t);
		else cout<<calc(s,"L", t);
		cout<<endl;
	}
	
}

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()){
    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(t=="U")s[0]-=0x20;
    else if(t=="L")s[0]+=0x20;
    else for(i=1;i<s.length();i++)
      if(between('A',s[i],'Z'))s.insert(i,1,'_'),s[++i]+=0x20;
    cout << s << endl;
  }
  }
}