POJ 3157 Java vs C++文字列処理


http://poj.org/problem?id=3157
 
Java vs C++
Time Limit: 2000 MS
 
メモリLimit: 65536 K
Description
Apologists of Java and C++can arge for houss proving each other that their programming lagage is the best one.Java people will tel that their programs are clearer and less prone to errors、while C++people will laugh at their inability to instantiate an array of genenersics or tell them thththaaaaaaat their programs arararassslolololong source code.Another isisaaaaathJavaand andC++people coulle coulle cololololololololololololololololololololololololololololololololololololololololololololole cococococococommtttttttttttttttttttttttttttttttttwritten starting from thesmall letter,and the follwing ones are written starting from the capital letter,no separators arused.All other letters are small.Examples of a Java dentifer are are are are  javaIdentifierlongAndMnemonicIdentifier、  name、  nEERC.Unike them,C++people use only small letters in their identifers.To separate words they use unders score character'_'.Examples of C+identifers are  c_identifierlong_and_mnemonic_identifier、  name (you see that when there is just one word Java and C++people agree)  n_e_e_r_c.You are writing a translator that is intenslate C+programs to Java and vice versa.Of course、identifers in the transplated program must be formated due to lagge rules-others wise people will never like your trans lator.The first thing you would like to write is and ideantifer trine.Gine.Gindentifirs。it would detect whether it is Java dentifer or C+dentifer and tranother it to another dialect.If it is neither,then your routine short an error.Translation must preserve the order of worlands thers mure therstrade of words therstrade changer
Input
The input file consists of one line that contains an identifer.It consists of letters of the English alphable and unders scores.Its length does not exced 100.
Output
If the input identifer is Java identifer,out put its C++version.If it is C++identifer,out put its Java version.If it is none,output"Error! instead.
Sample Input
sample input #1
long_and_mnemonic_identifier

sample input #2
anotherExample

sample input #3
i

sample input #4
bad_Style
Sample Output
sample output #1
longAndMnemonicIdentifier

sample output #2
another_example

sample output #3
i

sample output #4
Error!
/* Author : yan
 * Question : POJ 3157 Java vs C++
 * Data && Time : Thursday, December 30 2010 10:44 PM
*/
#include<stdio.h>
#define bool _Bool
#define true 1
#define false 0

char cache[150];
int len;

bool isJava()
{
	int i;
	for(i=0;cache[i]!='/0';i++)
	{
		if(cache[i]=='_') return false;
	}
	return true;
}
bool isCpp()
{
	int i;

	if(cache[len-1]=='_') return false;
	for(i=0;i<len;i++)
	{
		if(isupper(cache[i])) return false;
	}
	for(i=0;i<len-1;i++)
	{
		if(cache[i]=='_'&&cache[i+1]=='_') return false;
	}
	return true;
}
void java_to_cpp()
{
	int i;
	for(i=0;i<len;i++)
	{
		if( isupper(cache[i]) ) printf("_"),cache[i]+=32;
		printf("%c",cache[i]);
	}
}
void cpp_to_java()
{
	int i;
	for(i=0;i<len;i++)
	{
		if(cache[i]=='_') cache[i+1]-=32;
		else printf("%c",cache[i]);
	}
}
int main()
{
	scanf("%s",cache);
	len=strlen(cache);

	if( !islower( cache[0] ))
	{
		printf("Error!");
		goto exit;
	}
	if(isJava())
	{
		java_to_cpp();
	}
	else if(isCpp())
	{
		cpp_to_java();
	}
	else
	{
		printf("Error!");
		//goto exit;
	}
	exit:;

	return 0;
}