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>
using namespace std;

string str;
char ch;

void UCC(){
  string s = "";
  int size = str.size();

  if('a' <= str[0] && str[0] <= 'z') s += (str[0]-32);
  else s += str[0];

  for(int i = 1 ; i < size ; i++){
    if(str[i] == '_') continue;
    
    if(str[i-1] == '_'){
      if('A' <= str[i] && str[i] <= 'Z'){
	s += (str[i]+32);
      }
      else s += str[i];

      continue;      
    }
    s += str[i];
  }

  str = s;
}

void LCC(){
  string s = "";
  int size = str.size();

  if('A' <= str[0] && str[0] <= 'Z') s += (str[0]-32);
  else s += str[0];

  for(int i = 1 ; i < size ; i++){
    if(str[i] == '_') continue;

    if(str[i-1] == '_'){
      if('a' <= str[i] && str[i] <= 'z'){
	s += (str[i]-32);
      }
      else{
	s += str[i];
      }

      continue;      
    }
    s += str[i];
  }

  str = s;
}

void USC(){
  string s = "";
  int size = str.size();

  for(int i = 0 ; i < size ; i++){
    if('A' <= str[i] && str[i] <= 'Z'){
      if(i == 0){
	s += (str[i]+32);
      }else{
	s += '_';
	s += (str[i]+32);
      }
    }
    else{
      s += str[i];
    }
  }

  str = s;
}

int main(){

  while(true){
    cin >> str >> ch;
    
    if(ch == 'X') break;

    switch(ch){
    case 'L':
      LCC();
      break;

    case 'U':
      UCC();
      break;

    case 'D':
      USC();
      break;
    }

    cout << str << endl;
  }

  return 0;
}

C++: #include<vector>
#include<list>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
  int i;
  char c;
  string s;
  while(cin>>s>>c&&c!='X'){
    if(s.find('_')!=string::npos){
      if(c=='U'){
	s[0]+='A'-'a';
	while(s.find('_')!=string::npos){
	  s[s.find('_')+1]+='A'-'a';
	  s.erase(s.find('_'),1);
	}
      }else if(c=='L'){
	while(s.find('_')!=string::npos){
	  s[s.find('_')+1]+='A'-'a';
	  s.erase(s.find('_'),1);
	}
      }
    }else if('a'<=s[0]&&s[0]<='z'){
      if(c=='U'){
	s[0]+='A'-'a';
      }else if(c=='D'){
	for(i=0;i<(int)s.length();i++){
	  for(;i<(int)s.length()&&'a'<=s[i]&&s[i]<='z';i++);
	  if(i!=(int)s.length()){
	    s[i]+='a'-'A';
	    s.insert(i,1,'_');
	  }
	}
      }
    }else if('A'<=s[0]&&s[0]<='Z'){
      if(c=='L'){
	s[0]+='a'-'A';
      }else if(c=='D'){
	s[0]+='a'-'A';
	for(i=0;i<(int)s.length();i++){
	  for(;i<(int)s.length()&&'a'<=s[i]&&s[i]<='z';i++);
	  if(i!=(int)s.length()){
	    s[i]+='a'-'A';
	    s.insert(i,1,'_');
	  }
	}
      }
    }
    cout<<s<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<vector>
using namespace std;
bool isAL(char ch){ return ('A'<=ch&&ch<='Z'); }
bool isAl(char ch){return ('a'<=ch&&ch<='z');}
char change(char ch){
  if(isAl(ch))ch=ch-'a'+'A';
  return ch;
}

char change2(char ch){
  if(isAL(ch))ch=ch-'A'+'a';
  return ch;
}

string str,type;
vector<string> t;

int main(){
  while(1){
    cin>>str>>type;
    if(type=="X")break;
    t.clear();
    string tmp="";
    tmp.push_back(str[0]);
    str.push_back('_');
    for(int i=1;i<(int)str.size();i++){
      if(str[i]=='_'){
	t.push_back(tmp);
	tmp="";
      }else if(isAL(str[i])){
	t.push_back(tmp);
	tmp="";
	tmp.push_back(str[i]);
      }else{
	tmp.push_back(str[i]);
      }
    }
    for(int i=0;i<(int)t.size();i++){
      if(type=="L"){
	if(i)t[i][0]=change(t[i][0]);
	else t[i][0]=change2(t[i][0]);
      }else if(type=="U"){
	t[i][0]=change(t[i][0]);
      }else{
	t[i][0]=change2(t[i][0]);
      }
      
      if(i)cout<<(type=="D"?"_":"");
      cout<<t[i];
    }
    cout<<endl;
  }
  return 0;
}

C++: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class t>
using table = vector<vector<t>>;
const ld eps = 1e-9;

//// < "d:\d_download\visual studio 2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual studio 2015\projects\programing_contest_c++\debug\b.txt"
struct ToUpper {
	char operator()(char c) { return toupper(c); }
};
vector<string> split(const string &str, char delim) {
	vector<string> res;
	size_t current = 0, found;
	while ((found = str.find_first_of(delim, current)) != string::npos) {
		res.push_back(string(str, current, found - current));
		current = found + 1;
	}
	res.push_back(string(str, current, str.size() - current));
	return res;
}
int main() {
	while (1) {
		string st; cin >> st;
		char c; cin >> c;
		if (c == 'X')break;
		else {
			vector<string>names(1);
			if (st.find('_') == string::npos) {
				int now = 0;
				for (int i = 0; i < st.size(); ++i) {
					if (i&&st[i] >= 'A'&&'Z' >= st[i]) {
						names.push_back(string());
						now++;
					}
					names[now].push_back(char(tolower(st[i])));
				}
				
			}
			else {
				names = split(st, '_');
			}
			if (c == 'U') {
				for (int i = 0; i < names.size(); ++i) {
					for (int j = 0; j < names[i].size(); ++j) {
						if (j)cout << names[i][j];
						else cout << char(toupper(names[i][j]));
					}
				}
				cout << endl;
			}
			else if (c == 'L') {
				for (int i = 0; i < names.size(); ++i) {
					for (int j = 0; j < names[i].size(); ++j) {
						if (!i||j)cout << names[i][j];
						else cout << char(toupper(names[i][j]));
					}
				}
				cout << endl;
			}
			else {
				for (int i = 0; i < names.size(); ++i) {
					for (int j = 0; j < names[i].size(); ++j) {
						cout << names[i][j];

					}
					if (i == names.size() - 1)cout << endl;
					else cout << '_';
				}
			}
		}
	}
	return 0;
}