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;

void solve()
{
	string s;
	char c;
	while(cin >> s >> c)
	{
		string ans;
		if(c == 'X')
		{
			break;
		}
		else if(c == 'L')
		{
			if('A' <= s[0] && s[0] <= 'Z')
			{
				ans += s[0] - 'A' + 'a';
			}
			else
			{
				ans += s[0];
			}
			bool underbar_flag = false;
			for(int i = 1; i < s.size(); ++i)
			{
				if(underbar_flag)
				{
					ans += s[i] - 'a' + 'A';
					underbar_flag = false;
				}
				else if(s[i] != '_')
				{
					ans += s[i];
				}
				else if(s[i] == '_')
				{
					underbar_flag = true;
				}
			}
		}
		else if(c == 'U')
		{
			if('a' <= s[0] && s[0] <= 'z')
			{
				ans += s[0] - 'a' + 'A';
			}
			else
			{
				ans += s[0];
			}
			bool underbar_flag = false;
			for(int i = 1; i < s.size(); ++i)
			{
				if(underbar_flag)
				{
					ans += s[i] - 'a' + 'A';
					underbar_flag = false;
				}
				else if(s[i] != '_')
				{
					ans += s[i];
				}
				else if(s[i] == '_')
				{
					underbar_flag = true;
				}
			}
		}
		else if(c == 'D')
		{
			if('A' <= s[0] && s[0] <= 'Z')
			{
				ans += s[0] - 'A' + 'a';
			}
			else
			{
				ans += s[0];
			}
			for(int i = 1; i < s.size(); ++i)
			{
				if('A' <= s[i] && s[i] <= 'Z')
				{
					ans += '_';
					ans += s[i] - 'A' + 'a';
				}
				else
				{
					ans += s[i];
				}
			}
		}
		cout << ans << endl;
	}
}

int main()
{
	solve();
	return(0);
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>

using namespace std;

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define BW(a,x,b) ((a)<=(x)&&(x)<=(b))
#define F first
#define S second
#define INF 1 << 30

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

string name;
char type;

int main(){
  while(cin >> name >> type){
    if(type == 'X') break;
    if(name.find("_") != std::string::npos){
      if(type == 'D') goto e;
      if(type == 'U') name[0] -= 32;
      rep(i, name.size()){
	if(name[i] != '_') continue;
	name.erase(name.begin()+i);
	name[i] -= 32;
      }
    }else{
      if(type == 'L' ){ if(name[0] < 97) name[0] += 32;}
      else if(type == 'U'){ if( name[0] > 96) name[0] -= 32;}
      else{
	if(name[0] < 97) name[0] += 32;
	rep(i, name.size()){
	  if(name[i] > 96) continue;
	  name[i] += 32;
	  name.insert(i, "_");
	}
      }
    }
  e:
    cout << name << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
  string name,type,ans;
  string upper="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  string under="_";
  int start;
  while(cin>>name>>type,type!="X"){
    vector<string>word;
    vector<string>::iterator it;
    start=0;
    if(name.find("_")!=string::npos){
      for(int i=0;i<name.size();i++){
	if(under.find(name[i])!=string::npos){
	  word.push_back(name.substr(start,i-start));
	  start=i+1;
	}
      }
      word.push_back(name.substr(start,name.size()-start));
    }else{
      if(upper.find(name[0])!=string::npos)name[0]+='a'-'A';
      for(int i=0;i<name.size();i++){
	if(upper.find(name[i])!=string::npos){
	  name[i]+='a'-'A';
	  word.push_back(name.substr(start,i-start));
	  start=i;
	}
      }
      word.push_back(name.substr(start,name.size()-start));
    }
    ans="";
    for(it=word.begin();it!=word.end();it++){
      if(it==word.begin()){
	if(type=="U")(*it)[0]+='A'-'a';
	ans+=*it;
	continue;
      }
      if(type=="D")*it="_"+*it;
      else (*it)[0]+='A'-'a';
      ans+=*it;
    }
    cout<<ans<<endl;
  }
}