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

int main(){

  string s;
  char c;
  int n = 'a' - 'A';

  while(true){
    cin >> s >> c;
    if(c == 'X') break;

    string ss = "";

    if(c == 'U'){
      if(s[0] <= 'z' && s[0] >= 'a') s[0] -= n;
      for(int i=0;i<s.size();i++){
	if(s[i] == '_'){
	  s[i+1] -= n;
	  continue;
	}
	ss += s[i];
      }
    }

    if(c == 'L'){
      if(s[0] <= 'Z' && s[0] >= 'A') s[0] += n;
      for(int i=0;i<s.size();i++){
	if(s[i] == '_'){
	  s[i+1] -= n;
	  continue;
	}
	ss += s[i];
      }
    }

    if(c == 'D'){
      if(s[0] <= 'Z' && s[0] >= 'A') s[0] += n;
      for(int i=0;i<s.size();i++){
	if(s[i+1] <= 'Z' && s[i+1] >= 'A'){
	  ss += s[i];
	  ss += '_';
	  s[i+1] += n;
	  continue;
	}
	ss += s[i];
      }
    }

    cout << ss << endl;

  }
}

C++: #include<string>
#include<vector>
#include<iostream>
using namespace std;
int main(){
	string s;
	char c;
	while(cin>>s>>c,c!='X'){
		int now=1,pre=0;
		vector<string> box;
		while(1){
			if(now==s.size()){
				box.push_back(s.substr(pre,now-pre));
				break;
			}
			if(isupper(s[now])){
				box.push_back(s.substr(pre,now-pre));
				pre=now;
			}else if(s[now]=='_'){
				box.push_back(s.substr(pre,now-pre));
				now++;
				pre=now;
			}
			now++;
		}
		for(int i=0;i<box.size();i++){
			string t=box[i];
			char d=t[0];
			if(c=='L'){
				if(i==0&&isupper(d)){
					t[0]=tolower(d);
				}
				if(i!=0&&islower(d)){
					t[0]=toupper(d);
				}
			}else if(c=='U'){
				if(islower(d))
				t[0]=toupper(d);
				
			}else if(c=='D'){
				if(isupper(d))
				t[0]=tolower(d);
				if(i!=0)
				cout<<'_';
			}
			cout<<t;
		}
		cout<<endl;
	//	for(int i=0;i<box.size();i++)
	//	cout<<box[i]<<endl;
		
		
	}
	return 0;
}

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

using namespace std;

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

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

C++: #include<iostream>
#include<vector>
#include<cctype>
#include<sstream>

using namespace std;

vector<string> parse(const string &str){
  vector<string> ret;

  stringstream ss;

  for(int i = 0; i <= str.size(); i++){
    if(i == str.size()){
      ret.push_back(ss.str());
      ss.str("");
    }else if(str[i] == '_'){
      ret.push_back(ss.str());
      ss.str("");
    }else if(isupper(str[i])){
      if(ss.str().size() > 0) ret.push_back(ss.str());
      ss.str("");
      ss << (char)tolower(str[i]);
    }else{
      ss << str[i];
    }
  }

  return ret;
}

string convert(const vector<string> &v, char dist){
  stringstream ss;

  bool first = true;

  for(int i = 0; i < v.size(); i++){
    string tmp = v[i];
    
    if(dist == 'U'){
      tmp[0] = toupper(tmp[0]);
    }else if(dist == 'L'){
      if(!first) 
	tmp[0] = toupper(tmp[0]);
    }else if(dist == 'D'){
      if(!first)
	tmp = "_" + tmp;
    }

    ss << tmp;
    first = false;
  }

  return ss.str();
}

int main(){
  string buff;
  char dist;

  while(cin >> buff >> dist, dist != 'X'){
    cout << convert(parse(buff), dist) << endl;
  }

  return 0;
}