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

int main(){
	char buf[101],result[500],type;
	int p,i;

	while(true){
		scanf("%s %c",buf,&type);
		if(type == 'X')break;

		p = 0;
		switch(type){
		case 'U':
			for(i = 0;buf[i] != '\0';){
				if(i == 0){
					if(buf[i] >= 97 && buf[i] <= 122){
						result[p++] = buf[i] - 32;
					}else{
						result[p++] = buf[i];
					}
					i++;
				}else if(buf[i] == '_'){
					i++;
					if(buf[i] >= 97 && buf[i] <= 122){
						result[p++] = buf[i] - 32;
					}else{
						result[p++] = buf[i];
					}
					i++;
				}else{
					if(buf[i] >= 65 && buf[i] <= 90){
						if(buf[i-1] >= 97 && buf[i-1] <= 122){
							result[p++] = buf[i];
						}else{
							result[p++] = buf[i] + 32;
						}
					}else{
						result[p++] = buf[i];
					}
					i++;
				}
			}
			result[p] = '\0';

			printf("%s\n",result);
			break;
		case 'L':
			for(i = 0;buf[i] != '\0';){
				if(i == 0){
					if(buf[i] >= 65 && buf[i] <= 90){
						result[p++] = buf[i] + 32;
					}else{
						result[p++] = buf[i];
					}
					i++;
				}else if(buf[i] == '_'){
					i++;
					if(buf[i] >= 97 && buf[i] <= 122){
						result[p++] = buf[i] - 32;
					}else{
						result[p++] = buf[i];
					}
					i++;
				}else{
					if(buf[i] >= 65 && buf[i] <= 90){
						if(buf[i-1] >= 97 && buf[i-1] <= 122){
							result[p++] = buf[i];
						}else{
							result[p++] = buf[i] + 32;
						}
					}else{
						result[p++] = buf[i];
					}
					i++;
				}
			}
			result[p] = '\0';

			printf("%s\n",result);
			break;
		case 'D':
			for(i = 0;buf[i] != '\0';){
				if(i == 0){
					if(buf[i] >= 65 && buf[i] <= 90){
						result[p++] = buf[i] + 32;
					}else{
						result[p++] = buf[i];
					}
					i++;
				}else if(buf[i] == '_'){
					result[p++] = buf[i];
					i++;
				}else{
					if(buf[i] >= 65 && buf[i] <= 90){
						if(buf[i-1] == '_'){
							result[p++] = buf[i] + 32;
						}else if(buf[i-1] >= 97 && buf[i-1] <= 122){
							result[p++] = '_';
							result[p++] = buf[i] + 32;
						}else{
							result[p++] = buf[i] + 32;
						}
					}else{
						result[p++] = buf[i];
					}
					i++;
				}
			}
			result[p] = '\0';


			printf("%s\n",result);
			break;
		}
	}
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std ;

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) (r).begin(),(r).end()
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)
#define vvl(v,w,h,init) vector<vector<ll>> v(w,vector<ll>(h,init))
#define mp(a,b) make_pair(a,b)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using dict = map<string,int>;
using pii = pair<int,int> ;
using pll = pair<ll,ll> ;

const int mod = 1000000007;
constexpr int inf = ((1<<30)-1)*2+1 ;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0,1,-1,1,-1};
const int dx[] = {0,-1,0,1,1,-1,-1,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

template<typename T>
void Unique(vector<T> &v){
  sort(all(v));
  v.erase(unique(all(v)),v.end());
}
template<typename T,typename U>
ll FindErase(T &v,U tar){
  ll cnt = 0;
  for(auto it = v.begin(); it != v.end();){
    if(*it == tar){
      it = v.erase(it);
      ++cnt;
    }
    else{
      ++it;
    }
  }
  return cnt;
}

template<typename T>
bool SuffixErase(T &v,size_t suf){
  if(suf > v.size()) return false;
  for(auto it = v.begin(); it != v.end();){
    if(distance(v.begin(),it) == suf){
      v.erase(it);
      return true;
    }
    else{
      ++it;
    }
  }
  return false;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s;
  char c;
  while(cin >> s >> c && c != 'X'){
    if(c != 'D'){
      rep(i,s.size()){
        if(s[i] == '_'){
          s[i+1] = toupper(s[i+1]);
        }
      }
      if(c == 'U'){
        s[0] = toupper(s[0]);
      }
      else{
        s[0] = tolower(s[0]);
      }
      FindErase(s,'_');
    }
    else{
      string ans ;
      rep(i,s.size()){
        if(isupper(s[i]) && i != 0){
          ans +=  "_";
        }
        ans += tolower(s[i]);
      }
      s = ans;
    }
    cout << s << endl;
  }
  return 0;
}