E: C ++ and Java
Time limit:
2000 ms
Memory limit:
65536kb
Description
In the Baidu star Post Bar, Java fans and C ++ fans can always argue for which of the two languages can be better discussed for a few hours. Java enthusiasts will say that their programs are more clean and error-free. C ++ fans will laugh at the slow Java program and the code is very long.
Another argument that Java and C ++ cannot reach an agreement is naming. In Java, a variable name composed of multiple words should be named in the following format: the first word starts with a lowercase letter, and the other words start with an uppercase letter, there is no separator between a word and a word. All letters except the first letter of a word are in lower case. For example: javaidentifier, longandmnemonicidentifier, name, Baidu.
Different from Java, C ++ uses lower-case letters and "_" is used as the separator between words. For example: c_identifier, long_and_mnemonic_identifier, name (when the name contains only one word, Java and C ++ are named the same), B _a_ I _d_u.
Your task is to write a program that can convert C ++ and Java programs into one another. Of course, the variable names in the converted program must comply with the naming rules of the language. Otherwise, no one will like your converter.
The first thing you need to do is write a variable name converter. Given a variable name, you must first check whether it is Java or C ++, and then convert it into another naming format. If the two are not, your program will report an error. The conversion process must maintain the original word order. Only uppercase and lowercase letters can be changed and underscores can be added or deleted.
Input
The input has only one row. It is a variable name, which contains letters and underscores. The length cannot exceed 100.
Output
If the input is a Java variable name, the corresponding C ++ format is output. For C ++, the corresponding Java format is output. If either of them is not, "error!" Is output !".
Sample Input
Input Example 1:
Long_and_mnemonic_identifier
Input Example 2:
Anotherexample
Input Example 3:
I
Input Example 4:
Bad_style
Sample output
Output Example 1:
Longandmnemonicidentifier
Output Example 2:
Another_example
Output Example 3:
I
Output Example 4:
Error!
[Answer]
#include <iostream>#include <string>#include <cctype>using namespace std;int main(void){string strSample;string strCplus;string strJava;string strTransRslt;int strLen = 0;bool bCPlusJava = false; int iCnt = 0;string strTransFun(string strSource, bool bCJava);cin >> strSample;if( strSample[0] < 'a' || strSample[0] >'z' || strSample[strSample.length()-1] =='_'){cout << "Error!" << endl;return -1;}else{strLen = strSample.length();for(int k = 0; k < strLen; k++){if( (strSample[k] >= 'A') && (strSample[k] <='Z') && strSample.find('_') != string::npos){cout << "Error!" << endl;return -1;}if( !( (strSample[k] >= 'a' && strSample[k] <= 'z') || (strSample[k] >= 'A' && strSample[k] <= 'Z')) && strSample[k] != '_'){cout << "Error!" << endl;return -1;}}if( strSample.find("__") != string::npos){cout << "Error!" << endl;return -1;}if( strSample.find('_') != string::npos ) {bCPlusJava = true;strTransRslt = strTransFun(strSample,bCPlusJava);}else{for( int i = 0; i < strLen; i++){if(strSample[i] >= 'A' && strSample[i] <= 'Z') {bCPlusJava = false;strTransRslt = strTransFun(strSample,bCPlusJava);break;}if(strSample[i] >= 'a' && strSample[i] <= 'z'){iCnt++;}}if(iCnt == strLen){strTransRslt = strSample;}}}cout << strTransRslt << endl;return 0;}string strTransFun(string strSource, bool bCJava){string strReslt;int pos = 0;if(bCJava){for( int i = 0; i < (int)strSource.length(); i++){if(strSource[i] == '_'){pos = i;strSource.erase(pos,1);strSource[pos] = toupper(strSource[pos]);}}strReslt = strSource;}else{for( int i = 0; i < (int)strSource.length(); i++){if(strSource[i] >= 'A' && strSource[i] <= 'Z'){pos = i;strSource[pos] = tolower(strSource[pos]);strSource.insert(pos,1,'_');}}strReslt = strSource;}return strReslt;}
Note:
1) string processing mainly uses functions such as left, insert, and toupper.
2) the test data is very important. It starts with an underscore (it is related to the question of the question. It may be because the C ++ naming rules do not match, and some netizens have doubts) the combination of underscores (_) and larger letters, slide at the end, and multiple underscores (_) must be used to determine whether an error occurs. An error is returned! ".
3) The application of the isapha function may be g ++ (Baidu star connects to Peking University compiler) and may report an error, somehow.