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 len,i,flag;
  string str,out;
  char type;
  while(1){
    flag=0;
    out="";
    cin >> str >> type;
    len=str.size();
    if(type=='X') break;
    if(type=='U'){
      for(i=0;i<len;i++){
	if(i==0&&str[i]>='a') out+=str[i]-'a'+'A';
	else{
	  if(str[i]=='_'){
	    out+=str[i+1]-'a'+'A';
	    i++;
	  }else out+=str[i];
	}
      }
    }
    else if(type=='L'){
      /*      if(len==1){
	flag=1;
	if(str[0]>='a') out+=str[0]-'a'+'A';
	else out+=str[0]; 
	}*/
      //if(flag==0){
	for(i=0;i<len;i++){
	  if(i==0&&str[i]<'a') out+=str[i]-'A'+'a';
	  else{
	    if(str[i]=='_'){
	      out+=str[i+1]-'a'+'A';
	      i++;
	    }else out+=str[i];
	  }
	}
	//}
    }
    else if(type=='D'){
      for(i=0;i<len;i++){
	if(i==0&&str[i]<'a') out+=str[i]-'A'+'a';
	else{
	  if(str[i]<'a'){
	    out+='_';
	    out+=str[i]-'A'+'a';
	  }else out+=str[i];
	}
      }
    }
    cout << out << endl;
  }
  return 0;
}

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

int main()
{
  string str;
  char type;
  for(;cin>>str>>type,type!='X';)
    {      
      if(type=='U')
	{
	  for(int i=0;i<str.size();i++)
	    {
	      bool up=false;
	      if(i==0 || str[i]=='_')
		up=true;
	      if( str[i]=='_')
		i++;
	      if(up)
		cout<<(char)toupper(str[i]);
	      else
		cout<<str[i];
	    }
	}
      else if(type=='L')
	{
	  for(int i=0;i<str.size();i++)
	    {
	      if(str[i]=='_')
		{
		  i++;
		  cout<<(char)toupper(str[i]);
		}
	      else if(i==0)
		cout<<(char)tolower(str[i]);
	      else
		cout<<str[i];
	    }
	}
      else
	{
	  for(int i=0;i<str.size();i++)
	    {
	      if(i!=0 && isupper(str[i]))
		  cout<<"_";	      
	      cout<<(char)tolower(str[i]);	      
	    }
	}
      cout<<endl;
    }

}

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

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;
}

C++: #include <iostream>
#include <vector>
#include <cctype>
using namespace std;
int main(){
  string s,type;

  while(cin>> s >> type, type!="X" ){
    int st=0;
    vector<string> v;
    for( int i=0;i<(int)s.length();i++ ){
      if( i==s.length()-1 ){
	v.push_back( s.substr(st) ); break;
      }
      if( isupper(s[i]) ){
	s[i] = tolower(s[i]);
	if( i>0 )
	  v.push_back( s.substr(st,i-st) );
	st = i;
      }
      if( s[i]=='_' ){
	v.push_back( s.substr(st, i-st) );
	st = i+1;
      }
    }

    //#define DEBUG
#ifdef DEBUG
    cout << endl;
    cout << s << " => " << type << endl;
    for( int i=0;i<v.size();i++ )
      cout << " [" << v[i] << "]";
    cout << endl;
    cout << "[";
#endif
    for( int i=0;i<v.size();i++ ){
      if( (type=="L"&&i>0) || type=="U" ){
	v[i][0] = toupper(v[i][0]);
      }
      if( type=="D" && i>0 )
	cout << "_";
      cout << v[i];
    }
    cout << endl;

#ifdef DEBUG
    cout << "]";
#endif
  }
  return 0;
}

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

int main(){
  string str;
  char S;
  while(1){
    cin>>str;
    cin>>S;

    if(S=='X') break;

    else  if(S=='L'){
      for(int i=0;i<str.size();i++){
	if(str[i]=='_') str[i+1]-=32;
      }
      for(int i=0;i<str.size();i++){
	if(str[i]=='_') continue;
	else cout<<str[i];
      }
      cout<<endl;
    }
    
    else  if(S=='U') {
      if(str[0]>='a' && str[0]<='z') str[0]-=32;
    
    for(int i=0;i<str.size();i++){
    if(str[i]=='_') str[i+1]-=32;
  }
  for(int i=0;i<str.size();i++){
	if(str[i]=='_') continue;
	else cout<<str[i];
      }
      cout<<endl;
      
    }
    
  else  if(S=='D'){
    for(int i=0;i<str.size();i++){
      if(str[i]>=65 && str[i]<=90){
	str[i]+=32;
      }
      if(i>0 && str[i+1]>=65 && str[i+1]<=90){
	cout<<str[i]<<"_";
      }
      else cout<<str[i];
    }
    cout<<endl;
  }
  }
  return 0;
  }