Skip to content

Instantly share code, notes, and snippets.

@ylogx
Last active December 24, 2015 22:59
Show Gist options
  • Select an option

  • Save ylogx/6876774 to your computer and use it in GitHub Desktop.

Select an option

Save ylogx/6876774 to your computer and use it in GitHub Desktop.
P1 - translator
/*
* foobar - Shubham Chaudhary
* Nitul Datt
TranslatorIn 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.InputN, Number of testcases 0<N<=50N lines, that contains an identifier. It consists of letters of the English alphabet and underscores. Its length does not exceed 100.OutputIf 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:4long_and_mnemonic_identifieranotherExampleibad_StyleSample Output:longAndMnemonicIdentifieranother_exampleiError!
*/
#include <stdio.h>
#include <stdlib.h>
int fastread()
{
unsigned int input;
char c=0;
while (c<33){
c=getchar_unlocked();
//printf("\nWHILE: c is %c",c);
}
input=0;
while (c>33)
{
//printf("\nWHILE 2 : c is %c",c);
input=input*10+c-'0';
//printf("\n%d * 10 + %d - %d",input,c,'0');
c=getchar_unlocked();
}
//printf("\nInput is : %d",input);
return input;
}
int issmall(char in){
int i=(int)(in);
if(i>=97 && i<=122){
return 1;
}
return 0;
}
int iscaps(char in){
int i=(int)(in);
if(i>=65 && i<=90){
return 1;
}
return 0;
}
int main(){
//foobar
int n=fastread();
while(n--){
int i=0,j=0;
int valid=1;
char str[101];
char out[101];
scanf("%s",str);
if(iscaps(str[i])){
printf("Error!\n");
valid=0;
}
while(str[i]!='\0' && valid){
//printf("-----------------------loc1----\n");
if(iscaps(str[i])){
//printf("_%c",c+32);
out[j]='_';
j+=1;
out[j]=str[i]+32;
}
else if(str[i]=='_'){
//c=getchar_unlocked();
i+=1;
if(iscaps(str[i])){
printf("Error!\n");
//out[i]='\0';
valid=0;
break;
}else
//printf("%c",c-32);
out[j]=str[i]-32;
}
else{
//printf("%c",c);
out[j]=str[i];
}
//c=getchar_unlocked();
i++;
j++;
}
out[j]='\0';
if(valid){
//printf("-----------------------loc2----\n");
printf("%s",out);
printf("\n");
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment