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 <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <sstream>
#include <cassert>
using namespace std;
static const double EPS = 1e-5;
typedef long long ll;
typedef pair<int,int> PI;
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define MP make_pair
#define PB push_back

string str;
char type;

void pr(int pos){
  if(str[pos]=='_')return;
  if(pos==0 || str[pos-1]=='_' || isupper(str[pos])){
    switch(type){
    case 'U':
      cout<<(char)toupper(str[pos]);
      break;
    case 'L':
      if(pos==0)cout<<(char)tolower(str[pos]);
      else cout<<(char)toupper(str[pos]);
      break;
    case 'D':
      if(pos==0)cout<<(char)tolower(str[pos]);
      else cout<<"_"<<(char)tolower(str[pos]);
      break;
    }
  }else{
    cout<<str[pos];
  }
}

main(){
  while(cin>>str){
    cin>>type;
    if(type=='X')break;
    rep(i,str.size()){
      pr(i);
    }
    cout<<endl;
  }
}

C++: #include <iostream>
#include <string>
using namespace std;

string check(string str) {
  string str2;
  if('A'<=str[0] && str[0] <='Z') {
    str2 +=  str[0] +32;
    for(int i=1;i<str.size();i++) {
      if('A' <=str[i] && str[i] <= 'Z') {
	str2 += '_';
	str2 += str[i] + 32;
      }
      else str2  += str[i];
    }
    return str2;
  }

  
  for(int i=0;i<str.size();i++)
    if(str[i] == '_') return str;

  for(int i=0;i<str.size();i++) {
    if('A'<=str[i] && str[i] <='Z') {
      str2 += '_';
      str2 += str[i] + 32;
    }
    else str2 += str[i];
  }

  return str2;

}

int main() {

  while(1) {
    string str;
    cin >> str;
    string ch;
    cin >> ch;
    if(ch == "X") break;
    
    str= check(str);
    //    cout << str << endl;

    if(ch == "D") cout << str << endl;
    
else if(ch == "L") {
      cout << str[0];
      for(int i=1;i<str.size();i++) {
	if(str[i] == '_') {
	  i++;
	  str[i] -= 32;
	  cout << str[i];
	}
	else cout << str[i];
      }
      cout << endl;
    }
    else if(ch == "U"){
      str[0] -= 32;
      cout << str[0];
      for(int i=1;i<str.size();i++) {
	if(str[i] == '_') {
	  i++;
	  str[i] -= 32;
	  cout << str[i];
	}
	else {
	  cout << str[i];
	}
      }
      cout << endl;
    }

  }

  return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <ctype.h>

#define rep(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define all(v) (v).begin(), (v).end()
#define rev(s) (s).rbegin(), (s).rend()
#define MP make_pair
#define X first
#define Y second

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<double, double> dP;

const int INF = 100000000;
const double EPS = 1e-10;

vector<string> parse(string s){
	int idx = 0;
	vector<string> words;
	FOR(i, 1, s.size()){
		if(s[i] == '_' || (s[i] >= 'A' && s[i] <= 'Z')){
			words.push_back(s.substr(idx, i-idx));
			if(s[i] == '_')i++;
			idx = i;
		}
	}
	words.push_back(s.substr(idx, s.size()-idx));

	return words;
}

string ChangeCamelCase(vector<string> words, char type){
	string res;
	rep(i, words.size()){
		if(type == 'D' || (type == 'L' && i == 0)) words[i][0] = tolower(words[i][0]);
		else words[i][0] = toupper(words[i][0]);

		res += words[i];

		if(type == 'D' && (i != words.size()-1)) res+= '_';
	}

	return res;
}

int main(){
	string s;
	char t;

	while(cin >> s >> t, t != 'X'){
		cout << ChangeCamelCase(parse(s), t) << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <string>
using namespace std;
int main(){
  int i,len;
  char type;
  string name,out;
  while(1){
    out="";
    cin >> name >> type;
    if(type=='X') break;
    len=name.size();
    for(i=0;i<len;i++){
      if(i==0){
	if('A'<=name[i]&&name[i]<='Z'&&type!='U') out+=name[i]-'A'+'a';
	else if('a'<=name[i]&&name[i]<='z'&&type=='U') out+=name[i]-'a'+'A';
	else out+=name[i];
      }
      else{
	if(name[i]=='_'&&type!='D'){
	  out+=name[i+1]-'a'+'A';
	  i++;
	}
	else if('A'<=name[i]&&name[i]<='Z'&&type=='D'){
	  out+='_';
	  out+=name[i]-'A'+'a';
	}else out+=name[i];
      }
    }
    cout << out << endl;
  }
  return 0;
}