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>
#include <stdio.h>
#include <ctype.h>
#include <vector>
using namespace std;

int main(){
  vector < char > ch;
  vector < char > order;
  vector < char > ans;
  char odr,D,U,L,_;
  char moji;
  string str;
  int i,j,len,len2;
  int flag,p = 0;
  while(1){
    ans.clear();
    order.clear();
    
    cin >> str >> odr ;
    if( str == "EndOfInput" && odr == 'X' ) break;
    int l = str.size();
    flag = 0;
    for( i = 0; i < l; i++ ){
      if( str[i] == '_' ) flag = 1;
    }
  
    
    if( islower( str[0] ) &&  flag == 1 ) order.push_back('D');
    else if( isupper( str[0] ) ) order.push_back('U');
    else if( islower( str[0] ) ) order.push_back('L');

    for( i = 0; i < l; i++ ) ch.push_back( str[i] );
    
    order.push_back(odr);
    
    for( i = 0; i < l; i++ ){
      if( order[p] == 'U' && order[p+1] == 'U' )ans.push_back( str[i] );

      
      if( order[p] == 'U' && order[p+1] == 'L' ){
	if( i == 0 ) ans.push_back( tolower(str[0]) );
	else ans.push_back( str[i] );
      }

      if( order[p] == 'U' && order[p+1] == 'D' ){
	if( i == 0 ) ans.push_back( tolower(str[0]) );
	else if( isupper( str[i+1] ) ){
	  ans.push_back( str[i] );
	  ans.push_back('_');
	  ans.push_back( tolower( str[i+1] ) );
	  i++;
	}else ans.push_back( str[i] );

      }

      if( order[p] == 'L' && order[p+1] == 'U' ){
	if( i == 0 ) ans.push_back( toupper(str[0]) );
	else ans.push_back( str[i] );
      }

      if( order[p] == 'L' && order[p+1] == 'L' )ans.push_back( str[i] );


      if( order[p] == 'L' && order[p+1] == 'D' ){
	if( isupper( str[i+1] ) ){
	  ans.push_back( str[i] );
	  ans.push_back('_');
	  ans.push_back( tolower( str[i+1] ) );
	  i++;
	}else ans.push_back( str[i] );
      }

      if( order[p] == 'D' && order[p+1] == 'U' ){
	if( i == 0 ){
	  ans.push_back( toupper( str[0]) );
	}else{
	if( str[i] == '_' ){
	  ans.push_back( toupper(str[i+1]) );
	  i++;
	}else ans.push_back( str[i] );
	}
      }
      if( order[p] == 'D' && order[p+1] == 'L' ){
	if( str[i] == '_' ){
	  ans.push_back( toupper(str[i+1]) );
	  i++;
	}
	else ans.push_back( str[i] );
      }

      if ( order[p] == 'D' && order[p+1] == 'D' )ans.push_back( str[i] );
    }
 
    len2 = ans.size();
   
    for( i = 0; i < len2; i++ ) cout << ans[i];
    cout << endl;
   
    
  }
  
  return 0;
    }

C++: #include <iostream>
#include <string>
#include <vector>
#include <ctype.h>

using namespace std;

char get_type(string s)
{
	if (islower(s[0]))
		return s.find('_') != -1 ? 'D' : 'L';
	else
		return 'U';
}

vector<string> split_d(string s)
{
	vector<string> v;
	int p;
	for (; (p = s.find('_')) != -1; )
	{
		if (p > 0) v.push_back(s.substr(0, p));
		s = s.substr(p + 1);
	}
	if (!s.empty()) v.push_back(s);
	return v;
}

vector<string> split_c(string s)
{
	s[0] = tolower(s[0]);

	vector<string> v;
	for (int i = 0; i < s.length();)
	{
		if (isupper(s[i]))
		{
			string t = s.substr(0, i);
			v.push_back(t);
			s = s.substr(i);
			s[0] = tolower(s[0]);
			i = 0;
		}
		else
		{
			i++;
		}
	}
	if (!s.empty()) v.push_back(s);
	return v;
}

string make_d(vector<string> v)
{
	string s = "";
	for (int i = 0; i < v.size(); i++)
	{
		if (i != 0) s += "_";
		s += v[i];
	}
	return s;
}

string make_c(vector<string> v, bool upper_camel)
{
	string s = "";
	for (int i = 0; i < v.size(); i++)
	{
		if (i != 0 || upper_camel) v[i][0] = toupper(v[i][0]);
		s += v[i];
	}
	return s;
}

int main()
{
	string s;
	char c;
	for(;cin>>s>>c, c!='X';)
	{
		char type = get_type(s);

		vector<string> v;

		switch (type)
		{
		case 'L':
		case 'U':
			v = split_c(s);
			break;
		case 'D':
			v = split_d(s);
			break;
		}
		switch (c)
		{
		case 'L':
			cout << make_c(v, false);
			break;
		case 'U':
			cout << make_c(v, true);
			break;
		case 'D':
			cout << make_d(v);
			break;
		}
		cout << endl;
	}
	return 0;
}

C++: #include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<algorithm>
#include<complex>
#include<stack>
using namespace std ;
typedef vector<int> vi ;
typedef vector<vi> vvi ;
typedef vector<string> vs ;
typedef pair<int, int> pii; 
typedef long long ll ;

#define loop(i,a,b) for(int i = a ; i < b ; i ++)
#define rep(i,a) loop(i,0,a)
#define all(a) (a).begin(),(a).end()

int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};

int main(void){
  string buf;
  char com;
  while(cin>>buf>>com,com!='X'){
    string ans = "";
    switch(com){
    case 'L' :
      ans = tolower(buf[0]);
      loop(i,1,buf.size()){
	if(buf[i] == '_'){
	  ans += toupper(buf[++i]);
	}else{
	  ans += buf[i];
	}	
      }
      break;
    case 'U' :
      ans = toupper(buf[0]);
      loop(i,1,buf.size()){
	if(buf[i] == '_'){
	  ans += toupper(buf[++i]);
	}else{
	  ans += buf[i];
	}
      }
      break;
    case 'D' :
      ans = tolower(buf[0]);
      loop(i,1,buf.size()){
	if('A'<=buf[i] && buf[i]<='Z'){
	  ans += '_';
	  ans += tolower(buf[i]);
	}else{
	  ans += buf[i];
	}
      }
    }
    cout<<ans<<endl;
  }
}