C ++ and Java

Source: Internet
Author: User

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!

import java.io.BufferedInputStream;import java.util.*;public class T5 {public static void main(String[] args) {Scanner sc=new Scanner(new BufferedInputStream(System.in));while(sc.hasNextLine()){String s=sc.nextLine();fun(s);}}public static void fun(String s){StringBuffer bu=null;loop:if(s.charAt(0)>='a'&&s.charAt(0)<='z'){if(!s.contains("_")){bu=new StringBuffer(s);for(int i=0;i<s.length();i++){if(s.charAt(i)>='A'&&s.charAt(i)<='Z'){bu.insert(i,"_");}}System.out.println(bu);}else{bu=new StringBuffer(s);for(int i=0;i<s.length();i++){if(s.charAt(i)=='_'){if(s.charAt(i+1)>='A'&&s.charAt(i+1)<='Z'){System.out.println("ERROR");break loop;}else{bu.replace(i+1,i+2,String.valueOf((char)(s.charAt(i+1)-32)));}}}System.out.println(String.valueOf(bu).replace("_",""));}}else System.out.println("ERROR");}}

 

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: info-contact@alibabacloud.com 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.