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 <bits/stdc++.h>

#define reps(v, f, l) for (int v = (f), v##_ = (l); v < v##_; ++v)
#define rep(v, n) reps(v, 0, n)
#define lep(v, n) reps(v, 1, n + 1)

using namespace std;

typedef long long int lint;

static inline int in(){ int x; scanf("%d", &x); return x; }
static inline lint inl(){ lint x; scanf("%lld", &x); return x; }
template<typename T> void show(T& a, char del='\n', char last='\n'){ rep(i, a.size() - 1) cout << a[i] << del; cout << a[a.size() - 1] << last; }

int main()
{
  string n;
  char type;
  while (cin >> n >> type, type != 'X'){
    rep(i, n.size()){
      if (type == 'U'){
        if (i == 0){
          putchar(toupper(n[i]));
        }
        else if (n[i] == '_'){
          putchar(toupper(n[i + 1]));
          i++;
        }
        else {
          putchar(n[i]);
        }
      }
      if (type == 'L'){
        if (i == 0){
          putchar(tolower(n[i]));
        }
        else if (n[i] == '_'){
          putchar(toupper(n[i + 1]));
          i++;
        }
        else {
          putchar(n[i]);
        }
      }
      if (type == 'D'){
        if (i == 0){
          putchar(tolower(n[i]));
        }
        else if (isupper(n[i])){
          putchar('_');
          putchar(tolower(n[i]));
        }
        else {
          putchar(n[i]);
        }
      }
    }
    puts("");
  }
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <string.h>
#include <string>
#include <algorithm>
#include <cctype>
using namespace std;
int main(){
  char op;
  string str;
  while(cin >> str >> op, op != 'X'){
    int flag[str.size()];
    memset(flag, 0, sizeof(flag));
    for(int i = 0 ; i < (int)str.size() ; i++){
      if('A' <= str[i] && str[i] <= 'Z') flag[i] = 1;
      if(str[i] == '_') flag[i] = 2;
    }

    if(op != 'D'){
      if(op == 'U'){
	if('a' <= str[0] && str[0] <= 'z') str[0] -= 0x20; //toupper(str[0]);
	for(int i = 0 ; i < (int)str.size() ; i++){
	  
	  if(str[i] == '_'){
	    str[i] = 0;
	    if('a' <= str[i+1] && str[i+1] <= 'z') str[i+1] += 0x20; //toupper(str[i+1]);
	  }
	}
      }
      
      if(op == 'L'){
	if('A' <= str[0] && str[0] <= 'Z') str[0] -= 0x20;  //tolower(str[0]);
	for(int i = 0 ; i < (int)str.size() ; i++){
	  if(flag[i] == 2 && str[i] == '_'){
	    str[i] = 0;
	    if('a' <= str[i+1] && str[i+1] <= 'z') str[i+1] = toupper(str[i+1]);
	  }
	}
      }
      cout << str;
    }
    
    if(op == 'D'){
      for(int i = 0 ; i < (int)str.size() ; i++){
	if('A' <= str[i] && str[i] <= 'Z') str[i] = tolower(str[i]);
	if(flag[i] == 1 && i != 0){
	  cout << '_';
	}
	cout << str[i];
      }
    }  
    cout << endl;   
  }
  return 0;
}

C++: #include<iostream>
#include<string>
using namespace std;
string S; char C;
int main() {
	while (true) {
		cin >> S >> C; string D;
		if (C == 'X')break;
		if (C == 'L' || C == 'U') {
			for (int i = 0; i < S.size(); i++) {
				if (S[i] == '_') {
					i++; if (S[i] <= 'Z')D += S[i];
					else D += (char)(S[i] - 32);
				}
				else {
					if (S[i] > 'Z')D += S[i];
					else D += (char)(S[i] + 32);
				}
			}
			if (S[0] <= 'Z')S[0] += 32;
		}
		if (C == 'U') {
			if (S[0] > 'Z')S[0] -= 32;
		}
		if (C == 'D') {
			for (int i = 0; i < S.size(); i++) {
				if (S[i] <= 'Z' && i >= 1) { D += '_'; }
				D += S[i];
			}
		}
		cout << D << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <cctype>
using namespace std;
int main(){
  string s,type;

  while(cin>> s >> type, type!="X" ){
    int st=0;
    vector<string> v;
    for( int i=0;i<(int)s.length();i++ ){
      if( isupper(s[i]) ){
	s[i] = tolower(s[i]);
	if( i>0 )
	  v.push_back( s.substr(st,i-st) );
	st = i;
      }
      if( s[i]=='_' ){
	v.push_back( s.substr(st, i-st) );
	st = i+1;
      }
      if( i==s.length()-1 ){
	v.push_back( s.substr(st) ); break;
      }

    }

    //#define DEBUG
#ifdef DEBUG
    cout << endl;
    cout << s << " => " << type << endl;
    for( int i=0;i<v.size();i++ )
      cout << " [" << v[i] << "]";
    cout << endl;
    cout << "[";
#endif
    for( int i=0;i<v.size();i++ ){
      if( (type=="L"&&i>0) || type=="U" ){
	v[i][0] = toupper(v[i][0]);
      }
      if( type=="D" && i>0 )
	cout << "_";
      cout << v[i];
    }
    cout
#ifdef DEBUG
     << "]"
#endif
    << endl;

  }
  return 0;
}