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 <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
#define DEBUGP(val) cerr << #val << "=" << val << "\n"

using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> PI;



int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string s,t;
  while(cin>>s>>t&&t!="X") {
    vector<string> words;
    int last = 0;
    REP(i, 0, s.size()) {
      if (s[i] == '_') {
        words.push_back(s.substr(last, i - last));
        last = i + 1;
      }
      if (s[i] <= 'Z' && i > 0) {
        words.push_back(s.substr(last, i - last));
        last = i;
      }
    }
    words.push_back(s.substr(last));
    if (t == "U" || t == "L") {
      REP(i, 0, words.size()) {
        if (i > 0 || t == "U") words[i][0] = toupper(words[i][0]);
        else words[i][0] = tolower(words[i][0]);
        cout << words[i];
      }
      cout << endl;
    } else {
      REP(i, 0, words.size()) {
        if (i > 0) cout << "_";
        transform(words[i].begin(), words[i].end(), words[i].begin(), ::tolower);
        cout << words[i];
      }
      cout << endl;
    }
  }
}

C++: #include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define int long long
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

bool large(char c){
	if('A'<=c && c<='Z') return true;
	return false;
}

char change(char c){
	if('A'<=c && c<='Z') c+='a'-'A';
	else if('a'<=c && c<='z') c+='A'-'a';
	return c;
}

signed main(){
	while(1){
		string s,ans="";
		char c;
		cin>>s>>c;
		if(c=='X') break;
		else if(c=='D'){
			if(large(s[0])) ans+=change(s[0]);
			else ans+=s[0];
			rep(i,1,s.size()){
				if(large(s[i])) ans=ans+'_'+change(s[i]);
				else ans+=s[i];
			}
		}else if(c=='U'){
			if(!large(s[0])) ans+=change(s[0]);
			else ans+=s[0];
			rep(i,1,s.size()){
				if(s[i]=='_'){
					ans+=change(s[i+1]);
					i++;
				}else ans+=s[i];
			}
		}else if(c=='L'){
			if(large(s[0])) ans+=change(s[0]);
			else ans+=s[0];
			rep(i,1,s.size()){
				if(s[i]=='_'){
					ans+=change(s[i+1]);
					i++;
				}else ans+=s[i];
			}
		}
		o(ans);
	}
}

C++: #include <stdio.h>
#include <ctype.h>
#include <string.h>

int main(void){
	int i;
	char s[103], c;
	while(1){
		i = 0;
		while((s[i++] = getchar()) != '\n');
		s[i - 1] = '\0';
		c = s[strlen(s) - 1];
		s[strlen(s) - 2] = '\0';
		if(c == 'X') break;
		else if(c == 'U'){
			printf("%c",isupper(s[0])?s[0]:'A' + s[0] - 'a');
			for(i = 1;i < strlen(s);i++){
				if(s[i] == '_')
					printf("%c",'A' + s[++i] - 'a');
				else
					printf("%c",s[i]);
			}
		}
		else if(c == 'L'){
			printf("%c",islower(s[0])?s[0]:'a' + s[0] - 'A');
			for(i = 1;i < strlen(s);i++){
				if(s[i] == '_')
					printf("%c",'A' + s[++i] - 'a');
				else
					printf("%c",s[i]);
			}
		}
		else{
			printf("%c",islower(s[0])?s[0]:'a' + s[0] - 'A');
			for(i = 1;i < strlen(s);i++){
				if(isupper(s[i]))
					printf("_%c",'a' + s[i] - 'A');
				else if(s[i] == '_')
					printf("_%c",s[++i]);
				else
					printf("%c",s[i]);
			}
		}
		printf("\n");
	}
	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') {
			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 (D[0] <= 'Z')D[0] += 32;
		}
		if (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 {
					D += S[i];
				}
			}
			if (D[0] > 'Z')D[0] -= 32;
		}
		if (C == 'D') {
			for (int i = 0; i < S.size(); i++) {
				if (S[i] <= 'Z' && i >= 1) { D += '_'; }
				if (S[i] <= 'Z') { D += (char)(S[i] + 32); }
				else { D += S[i]; }
			}
		}
		cout << D << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <string>
using namespace std;
int main(void){
    string s1, s2;

    while(true){
        //scanf("%s %s", s1, s2);
        cin >> s1 >> s2;
        if(s2 == "X") break;

        if(s2 == "D"){
            for(int i = 1; i < s1.size(); i++){
                if(isupper(s1[i])) s1.insert(i++, "_");
            }
            for(int i = 0; i < s1.size(); i++){
            s1[i] = tolower(s1[i]);
            } 
            cout << s1 << endl;
        }
        else{
            for(int i = 0; i < s1.size(); i++){
                if(s1[i] == '_'){
                    s1[i + 1] = toupper(s1[i + 1]);
                    s1.erase(i, 1);
                }
            }     
            if(s2 == "U"){
                s1[0] = toupper(s1[0]);
                cout << s1 << endl;
            }
            else{
                s1[0] = tolower(s1[0]);
                cout << s1 << endl;
            }
        }
    }
}