描述
在百度之星的贴吧里面,Java的爱好者和C++的爱好者总是能为这两种语言哪个更好争论上几个小时。Java的爱好者会说他们的程序更加整洁且不易出错。C++的爱好者则会嘲笑Java程序很慢而且代码很长。
另一个Java和C++爱好者不能达成一致的争论点就是命名问题。在Java中一个多个单词构成的变量名应该按照如下格式命名:第一个单词的开头用小写字母,其余单词都以大写字母开头,单词与单词之间不加分隔符,除单词的首字母之外的字母一律使用小写。例如:javaIdentifier, longAndMnemonicIdentifier, name, bAIDU.
与Java不同C++的命名全都使用小写字母,在单词和单词之间使用“_”来作为分隔符。例如:c_identifier,long_and_mnemonic_identifier, name (当名字中只有一个单词的时候,Java与C++的命名是相同的), b_a_i_d_u.
你的任务就是写一个程序能让C++和Java程序相互转化。当然转换完成的程序中的变量名也要符合其语言的命名规则,否则的话是不会有人喜欢你的转换器的。
首先你要做的就是写一个变量名转换器。给出一个变量名,你要先检测是Java的还是C++的,然后把它转化为另一种命名格式。如果两种都不是,那么你的程序就要报错。转换过程必须保持原有的单词顺序,只能改变字母的大小写和增加或删除下划线。
输入
输入有且仅有一行,是一个变量名,其中包含字母和下划线,长度不超过100。
输出
如果输入的是Java变量名那么输出它对应的C++形式。如果是C++的则输出对应的Java的形式。如果两种都不是就输出“Error!”。
样例输入
输入样例1:
long_and_mnemonic_identifier
输入样例2:
anotherExample
输入样例3:
i
输入样例4:
bad_Style
样例输出
输出样例1:
longAndMnemonicIdentifier
输出样例2:
another_example
输出样例3:
i
输出样例4:
Error!
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String[] outStr = new String[200];
int i = 0;
outStr[i] = scan.next();
if (outStr[i].substring(0, 1).equals("_")
|| outStr[i].substring(0, 1).matches("[A-Z]")
|| outStr[i].contains("___")
|| outStr[i].contains("____")
|| outStr[i].contains("__")
|| outStr[i].substring(outStr[i].length() - 1,
outStr[i].length()).equals("_")
|| (outStr[i].contains("_") && (outStr[i].contains("A")
|| outStr[i].contains("B") || outStr[i].contains("C")
|| outStr[i].contains("D") || outStr[i].contains("E")
|| outStr[i].contains("F") || outStr[i].contains("G")
|| outStr[i].contains("H") || outStr[i].contains("I")
|| outStr[i].contains("J") || outStr[i].contains("K")
|| outStr[i].contains("L") || outStr[i].contains("M")
|| outStr[i].contains("N") || outStr[i].contains("O")
|| outStr[i].contains("P") || outStr[i].contains("Q")
|| outStr[i].contains("R") || outStr[i].contains("S")
|| outStr[i].contains("T") || outStr[i].contains("U")
|| outStr[i].contains("V") || outStr[i].contains("W")
|| outStr[i].contains("X") || outStr[i].contains("Y") || outStr[i]
.contains("Z")))) {
System.out.println("Error!");
}
else if (outStr[i].contains("_")) {
// int index = str.indexOf("_");
//
// outStr[i] = str.substring(0, index);
//
// str = str.substring(index + 1);
//
// outStr[i] = outStr[i] + str.substring(0, 1).toUpperCase()
// + str.substring(1);
if (outStr[i] != null) {
while (outStr[i].contains("_")) {
// if (outStr[i].contains("_")) {
int index = outStr[i].indexOf("_");
i++;
outStr[i] = outStr[i - 1].substring(0, index);
outStr[i - 1] = outStr[i - 1].substring(index + 1);
outStr[i] = outStr[i]
+ outStr[i - 1].substring(0, 1).toUpperCase()
+ outStr[i - 1].substring(1);
// }
}
System.out.println(outStr[i]);
}
}
else if (outStr[i].contains("A") || outStr[i].contains("B")
|| outStr[i].contains("C") || outStr[i].contains("D")
|| outStr[i].contains("E") || outStr[i].contains("F")
|| outStr[i].contains("G") || outStr[i].contains("H")
|| outStr[i].contains("I") || outStr[i].contains("J")
|| outStr[i].contains("K") || outStr[i].contains("L")
|| outStr[i].contains("M") || outStr[i].contains("N")
|| outStr[i].contains("O") || outStr[i].contains("P")
|| outStr[i].contains("Q") || outStr[i].contains("R")
|| outStr[i].contains("S") || outStr[i].contains("T")
|| outStr[i].contains("U") || outStr[i].contains("V")
|| outStr[i].contains("W") || outStr[i].contains("X")
|| outStr[i].contains("Y") || outStr[i].contains("Z")) {
// String[] outStr = new String[100];
// int i = 0;
//
// int index = outStr[i].indexOf("A");
// outStr[i] = outStr[i].substring(0, index);
// outStr[i] = outStr[i].substring(index);
// outStr[i] = outStr[i] + "_" + outStr[i].substring(0,
// 1).toLowerCase()
// + outStr[i].substring(1);
// System.out.println(outStr[i]);
if (outStr[i] != null) {
while (outStr[i].contains("A")) {
int index = outStr[i].indexOf("A");
i++;
outStr[i] = outStr[i - 1].substring(0, index);
outStr[i - 1] = outStr[i - 1].substring(index);
outStr[i] = outStr[i] + "_"
+ outStr[i - 1].substring(0, 1).toLowerCase()
+ outStr[i - 1].substring(1);
}
}
// index = str.indexOf("B");
// outStr[i] = str.substring(0, index);
// str = str.substring(index);
// outStr[i] = outStr[i] + "_" + str.substring(0, 1).toLowerCase()
// + str.substring(1);
// System.out.println(outStr[i]);
if (outStr[i] != null) {
while (outStr[i].contains("B")) {
int index = outStr[i].indexOf("B");
i++;
outStr[i] = outStr[i - 1].substring(0, index);
outStr[i - 1] = outStr[i - 1].substring(index);
outStr[i] = outStr[i] + "_"
+ outStr[i - 1].substring(0, 1).toLowerCase()
+ outStr[i - 1].substring(1);
}
}
if (outStr[i] != null) {
while (outStr[i].contains("C")) {
int index = outStr[i].indexOf("C");
i++;
outStr[i] = outStr[i - 1].substring(0, index);
outStr[i - 1] = outStr[i - 1].substring(index);
outStr[i] = outStr[i] + "_"
+ outStr[i - 1].substring(0, 1).toLowerCase()
+ outStr[i - 1].substring(1);
}
}
if (outStr[i] != null) {
while (outStr[i].contains("D")) {
int index = outStr[i].indexOf("D");
i++;
outStr[i] = outStr[i - 1].substring(0, index);
outStr[i - 1] = outStr[i - 1].substring(index);
outStr[i] = outStr[i] + "_"
+ outStr[i - 1].substring(0, 1).toLowerCase()
+ outStr[i - 1].substring(1);
}
}
if (outStr[i] != null) {
while (outStr[i].contains("E")) {
int index = outStr[i].indexOf("E");
i++;
outStr[i] = outStr[i - 1].substring(0, index);
outStr[i - 1] = outStr[i - 1].substring(index);
outStr[i] = outStr[i] + "_"
+ outStr[i - 1].substring(0, 1).toLowerCase()
+ outStr[i - 1].substring(1);
}
}
if (outStr[i] != null) {
while (outStr[i].contains("F")) {
int index = outStr[i].indexOf("F");
i++;
outStr[i] = outStr[i - 1].substring(0, index);
outStr[i - 1] = outStr[i - 1].substring(index);
outStr[i] = outStr[i] + "_"
+ outStr[i - 1].substring(0, 1).toLowerCase()
+ outStr[i - 1].substring(1);
}
}
if (outStr[i] != null) {
while (outStr[i].contains("G")) {
int index = outStr[i].indexOf("G");
i++;
outStr[i] = outStr[i - 1].substring(0, index);
outStr[i - 1] = outStr[i - 1].substring(index);
outStr[i] = outStr[i] + "_"
+ outStr[i - 1].substring(0, 1).toLowerCase()
+ outStr[i - 1].substring(1);
}
}
if (outStr[i] != null) {
while (outStr[i].contains("H")) {
int index = outStr[i].indexOf("H");
i++;
outStr[i] = outStr[i - 1].substring(0, index);
outStr[i - 1] = outStr[i - 1].substring(index);
outStr[i] = outStr[i] + "_"
+ outStr[i - 1].substring(0, 1).toLowerCase()
+ outStr[i - 1].substring(1);
}
}
if (outStr[i] != null) {
while (outStr[i].contains("I")) {
int index = outStr[i].indexOf("I");
i++;
outStr[i] = outStr[i - 1].substring(0, index);
outStr[i - 1] = outStr[i - 1].substring(index);
outStr[i] = outStr[i] + "_"
+ outStr[i - 1].substring(0, 1).toLowerCase()
+ outStr[i - 1].substring(1);
}
}
if (outStr[i] != null) {
while (outStr[i].contains("J")) {
int index = outStr[i].indexOf("J");
i++;
outStr[i] = outStr[i - 1].substring(0, index);
outStr[i - 1] = outStr[i - 1].substring(index);
outStr[i] = outStr[i] + "_"
+ outStr[i - 1].substring(0, 1).toLowerCase()
+ outStr[i - 1].substring(1);
}
}
if (outStr[i] != null) {
while (outStr[i].contains("K")) {
int index = outStr[i].indexOf("K");
i++;
outStr[i] = outStr[i - 1].substring(0, index);
outStr[i - 1] = outStr[i - 1].substring(index);
outStr[i] = outStr[i] + "_"
+ outStr[i - 1].substring(0, 1).toLowerCase()
+ outStr[i - 1].substring(1);
}
}
if (outStr[i] != null) {
while (outStr[i].contains("L")) {
int index = outStr[i].indexOf("L");
i++;
outStr[i] = outStr[i - 1].substring(0, index);
outStr[i - 1] = outStr[i - 1].substring(index);
outStr[i] = outStr[i] + "_"
+ outStr[i - 1].substring(0, 1).toLowerCase()
+ outStr[i - 1].substring(1);
}
}
if (outStr[i] != null) {
while (outStr[i].contains("M")) {
int index = outStr[i].indexOf("M");
i++;
outStr[i] = outStr[i - 1].substring(0, index);
outStr[i - 1] = outStr[i - 1].substring(index);
outStr[i] = outStr[i] + "_"
+ outStr[i - 1].substring(0, 1).toLowerCase()
+ outStr[i - 1].substring(1);
}
}
if (outStr[i] != null) {
while (outStr[i].contains("N")) {
int index = outStr[i].indexOf("N");
i++;
outStr[i] = outStr[i - 1].substring(0, index);
outStr[i - 1] = outStr[i - 1].substring(index);
outStr[i] = outStr[i] + "_"
+ outStr[i - 1].substring(0, 1).toLowerCase()
+ outStr[i - 1].substring(1);
}
}
if (outStr[i] != null) {
while (outStr[i].contains("O")) {
int index = outStr[i].indexOf("O");
i++;
outStr[i] = outStr[i - 1].substring(0, index);
outStr[i - 1] = outStr[i - 1].substring(index);
outStr[i] = outStr[i] + "_"
+ outStr[i - 1].substring(0, 1).toLowerCase()
+ outStr[i - 1].substring(1);
}
}
if (outStr[i] != null) {
while (outStr[i].contains("P")) {
int index = outStr[i].indexOf("P");
i++;
outStr[i] = outStr[i - 1].substring(0, index);
outStr[i - 1] = outStr[i - 1].substring(index);
outStr[i] = outStr[i] + "_"
+ outStr[i - 1].substring(0, 1).toLowerCase()
+ outStr[i - 1].substring(1);
}
}
if (outStr[i] != null) {
while (outStr[i].contains("Q")) {
int index = outStr[i].indexOf("Q");
i++;
outStr[i] = outStr[i - 1].substring(0, index);
outStr[i - 1] = outStr[i - 1].substring(index);
outStr[i] = outStr[i] + "_"
+ outStr[i - 1].substring(0, 1).toLowerCase()
+ outStr[i - 1].substring(1);
}
}
if (outStr[i] != null) {
while (outStr[i].contains("R")) {
int index = outStr[i].indexOf("R");
i++;
outStr[i] = outStr[i - 1].substring(0, index);
outStr[i - 1] = outStr[i - 1].substring(index);
outStr[i] = outStr[i] + "_"
+ outStr[i - 1].substring(0, 1).toLowerCase()
+ outStr[i - 1].substring(1);
}
}
if (outStr[i] != null) {
while (outStr[i].contains("S")) {
int index = outStr[i].indexOf("S");
i++;
outStr[i] = outStr[i - 1].substring(0, index);
outStr[i - 1] = outStr[i - 1].substring(index);
outStr[i] = outStr[i] + "_"
+ outStr[i - 1].substring(0, 1).toLowerCase()
+ outStr[i - 1].substring(1);
}
}
if (outStr[i] != null) {
while (outStr[i].contains("T")) {
int index = outStr[i].indexOf("T");
i++;
outStr[i] = outStr[i - 1].substring(0, index);
outStr[i - 1] = outStr[i - 1].substring(index);
outStr[i] = outStr[i] + "_"
+ outStr[i - 1].substring(0, 1).toLowerCase()
+ outStr[i - 1].substring(1);
}
}
if (outStr[i] != null) {
while (outStr[i].contains("U")) {
int index = outStr[i].indexOf("U");
i++;
outStr[i] = outStr[i - 1].substring(0, index);
outStr[i - 1] = outStr[i - 1].substring(index);
outStr[i] = outStr[i] + "_"
+ outStr[i - 1].substring(0, 1).toLowerCase()
+ outStr[i - 1].substring(1);
}
}
if (outStr[i] != null) {
while (outStr[i].contains("V")) {
int index = outStr[i].indexOf("V");
i++;
outStr[i] = outStr[i - 1].substring(0, index);
outStr[i - 1] = outStr[i - 1].substring(index);
outStr[i] = outStr[i] + "_"
+ outStr[i - 1].substring(0, 1).toLowerCase()
+ outStr[i - 1].substring(1);
}
}
if (outStr[i] != null) {
while (outStr[i].contains("W")) {
int index = outStr[i].indexOf("W");
i++;
outStr[i] = outStr[i - 1].substring(0, index);
outStr[i - 1] = outStr[i - 1].substring(index);
outStr[i] = outStr[i] + "_"
+ outStr[i - 1].substring(0, 1).toLowerCase()
+ outStr[i - 1].substring(1);
}
}
if (outStr[i] != null) {
while (outStr[i].contains("X")) {
int index = outStr[i].indexOf("X");
i++;
outStr[i] = outStr[i - 1].substring(0, index);
outStr[i - 1] = outStr[i - 1].substring(index);
outStr[i] = outStr[i] + "_"
+ outStr[i - 1].substring(0, 1).toLowerCase()
+ outStr[i - 1].substring(1);
}
}
if (outStr[i] != null) {
while (outStr[i].contains("Y")) {
int index = outStr[i].indexOf("Y");
i++;
outStr[i] = outStr[i - 1].substring(0, index);
outStr[i - 1] = outStr[i - 1].substring(index);
outStr[i] = outStr[i] + "_"
+ outStr[i - 1].substring(0, 1).toLowerCase()
+ outStr[i - 1].substring(1);
}
}
if (outStr[i] != null) {
while (outStr[i].contains("Z")) {
int index = outStr[i].indexOf("Z");
i++;
outStr[i] = outStr[i - 1].substring(0, index);
outStr[i - 1] = outStr[i - 1].substring(index);
outStr[i] = outStr[i] + "_"
+ outStr[i - 1].substring(0, 1).toLowerCase()
+ outStr[i - 1].substring(1);
}
}
System.out.println(outStr[i]);
}
else
System.out.println(outStr[i]);
}
}