Baidu star E: C ++ and Java

Source: Internet
Author: User
Tags uppercase letter java format

E: C ++ and Java

Time limit:

2000 ms

Memory limit:



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.


The input has only one row. It is a variable name, which contains letters and underscores. The length cannot exceed 100.


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:


Input Example 2:


Input Example 3:


Input Example 4:


Sample output

Output Example 1:


Output Example 2:


Output Example 3:


Output Example 4:



#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;}


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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.