Translator
In a country of Neverland there are two types of programmers, People who
program using a language X(X people) and people who program using a language Y(Y
people).
Both are equally good languages but there is no translator that could
translate X language to Y and vice versa.Apologists of X and Y can argue for hours proving each other that their
programming language is the best one.Y people will tell that their programs are clearer and less prone to errors,
while X people will laugh at their inability to instantiate an array of generics or tell them that their programs are slow
and have long source code. Another issue that X and Y people could never agree on is identifier naming.
In Y a multiword identifier is constructed in the following manner: the first word is written starting from the small letter, and the following
ones are written starting from the capital letter, no separators are used.
All other letters are small. Examples of a Y identifier are
yIdentifier, longAndMnemonicIdentifier, name,nEERC.Unlike them, X people use only small letters in their identifiers. To
separate words they use underscore character "_". Examples of X identifiers are x_identifier, long_and_mnemonic_identifier,
name (you see that when there is just one word X and Y people agree),
n_e_e_r_c.
Your job is to write a translator that is intended to translate programs in X
to Y and vice versa. Of course, identifiers in the translated program must be formatted due to its language rules - otherwise
people will never like your translator.
The first thing you would like to write is an identifier translation routine.
Given an identifier,
it would detect whether it is Y identifier or X identifier and translate it
to another dialect. If it is neither, then your routine should report an error.
Translation must preserve the order of words and must only change the case of
letters and/or add/remove underscores.
Input
N, Number of testcases 0<N<=50
N lines, that contains an identifier. It consists of letters of the English
alphabet and underscores. Its length does not exceed 100.
Output
If the input identifier is Y identifier, output its X version. If it is X
identifier, output its Y version. If it is none, output "Error!" instead.
Sample Input:
4
long_and_mnemonic_identifier
anotherExample
i
bad_Style
Sample Output:
longAndMnemonicIdentifier
another_example
i
Error!
import java.io.*; class CharDemo { public static void main(String a[]) throws IOException { BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); int test=Integer.parseInt(in.readLine()); String input[]=new String[test]; for(int i=0;i<test;i++) { input[i]=in.readLine(); } String output[]=new String[test]; for(int k=0;k<test;k++) { int m=0; char[] arr=input[k].toCharArray(); char temp[]=new char[arr.length+6]; for(int i=0;i<arr.length;i++) { if(Character.isUpperCase(arr[i])&&i!=0) { temp[m++]='_'; temp[m++]=Character.toLowerCase(arr[i]); } else if(arr[i]=='_') { if(Character.isLowerCase(arr[i+1])) temp[m++]=Character.toUpperCase(arr[++i]); else { m=0; for(int f=0;f<"Error!".length();f++) temp[m++]="Error!".charAt(f); i=arr.length+2; } } else temp[m++]=arr[i]; } output[k]=new String(temp); } for(int k=0;k<test;k++) System.out.println(output[k]); } }