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

#define iota(i,n,b,s) for(int i=int(b);i!=int((b)+(s)*(n));i+=(s))
#define range(i,n,m) iota(i,(((n)>(m))?((n)-(m)+1):((m)-(n)+1)),(n),((n)>(m)?-1:1))
#define rep(i,n) iota(i,(n),0,1)

#define INF (1e9)
#define EPS (1e-9)
#define cons(a,b) (make_pair(a,b))
#define car(a) (a.first)
#define cdr(a) (a.second)
#define cadr(a) (car(cdr(a)))
#define cddr(a) (cdr(cdr(a)))
#define all(a) a.begin(), a.end()
#define trace(var) cerr<<">>> "<<#var<<" = "<<var<<endl;

typedef long long Integer;
typedef double Real;

template<class S, class T>
ostream& operator<<(ostream& os, pair<S,T> p) {
  os << '(' << car(p) << ", " << cdr(p) << ')';
  return os;
}

template<class T>
ostream& operator<<(ostream& os, vector<T> v) {
  os << v[0];
  for (int i=1, len=v.size(); i<len; ++i) os << ' ' << v[i];
  return os;
}

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

inline bool isD(string s) { return s.find('_') != string::npos; }
inline bool isL(string s) { return !isD(s) && islower(s[0]); }
inline bool isU(string s) { return !isD(s) && isupper(s[0]); }

vector<string>
splitL(string s) {
  vector<string> ret;
  int idx = 0;
  rep(i, s.size()) {
    if (isupper(s[i])) {
      string w = char(tolower(s[idx])) + s.substr(idx + 1, i - idx - 1);
      ret.push_back(w);
      idx = i;
    }
  }
  string w = char(tolower(s[idx])) + s.substr(idx + 1, s.size() - idx - 1);
  ret.push_back(w);
  return ret;
}

vector<string>
splitU(string s) {
  return splitL(char(tolower(s[0])) + s.substr(1, s.size() - 1));
}

vector<string>
splitD(string s) {
  vector<string> ret;
  int idx = 0;
  rep(i, s.size()) {
    if (s[i] == '_') {
      ret.push_back(s.substr(idx, i - idx));
      idx = i + 1;
    }
  }
  ret.push_back(s.substr(idx, s.size() - idx));
  return ret;
}

string toL(vector<string>&v) {
  string r = v[0];
  for (int i=1; i < v.size(); ++i) {
    r += char(toupper(v[i][0])) + v[i].substr(1, v[i].size() - 1);
  }
  return r;
}

string toU(vector<string>&v) {
  string r = "";
  for (int i=0; i < v.size(); ++i) {
    r += char(toupper(v[i][0])) + v[i].substr(1, v[i].size() - 1);
  }
  return r;
}

string toD(vector<string>&v) {
  string r = v[0];
  for (int i=1; i < v.size(); ++i) {
    r += "_" + v[i];
  }
  return r;
}

int main() {

more:
  string s, t;
  cin >> s >> t;
  
  if (t == "X") return 0;

  vector<string> v;
  if (isL(s)) {
    v = splitL(s);
  } else if (isU(s)) {
    v = splitU(s);
  } else if (isD(s)) {
    v = splitD(s);
  }

  if (t == "U") {
    cout << toU(v) << endl;
  } else if (t == "L") {
    cout << toL(v) << endl;
  } else if (t == "D") {
    cout << toD(v) << endl;
  }

  goto more;
}

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( i==s.length()-1 ){
	v.push_back( s.substr(st) ); break;
      }
      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;
      }
    }
    /*
    cout << endl;
    for( int i=0;i<v.size();i++ )
      cout << " [" << v[i] << "]";
    cout << endl;
    */

    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>1 )
	cout << "_";
      cout << v[i];
    }
    cout << 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( i==s.length()-1 ){
	v.push_back( s.substr(st) ); break;
      }
      if( isupper(s[i]) ){
	s[i] = tolower(s[i]);
	v.push_back( s.substr(st,i-st) );
	st = i;
      }
      if( s[i]=='_' ){
	v.push_back( s.substr(st, i-st) );
	st = i+1;
      }
    }
    /*
    cout << endl;
    for( int i=0;i<v.size();i++ )
      cout << " " << v[i];
    cout << endl;
    */

    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>1 )
	cout << "_";
      cout << v[i];
    }
    cout << endl;

  }
  return 0;
}