forked from love1024/spoj-solution-with-explanation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJAVAC.cpp
69 lines (67 loc) · 1.06 KB
/
JAVAC.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <string>
using namespace std;
// used - https://github.com/t3nsor/SPOJ/blob/master/javac.cpp
bool java(string s)
{
int i;
for (i=0; i<s.length(); i++)
if (s[i]=='_')
return false;
if (s[0]<='Z')
return false;
return true;
}
bool cpp(string s)
{
int i;
for (i=0; i<s.length(); i++)
if (s[i]<='Z')
return false;
if (s[0]=='_')
return false;
if (s[s.length()-1]=='_')
return false;
for (i=0; i<s.length()-2; i++)
if (s[i]=='_' && s[i+1]=='_')
return false;
return true;
}
string translate_to_cpp(string s) //Java to C++
{
int i;
for (i=0; i<s.length(); i++)
if (s[i]<='Z')
{
char c=s[i];
s.insert(i,"_");
s[i+1]=c+32;
}
return s;
}
string translate_to_java(string s) //C++ to Java
{
int i;
for (i=0; i<s.length(); i++)
if (s[i]=='_')
{
s.erase(i,1);
s[i]=s[i]-32;
}
return s;
}
int main()
{
string s;
while (!cin.eof())
{
cin >> s;
if (java(s))
cout << translate_to_cpp(s) << endl;
else if (cpp(s))
cout << translate_to_java(s) << endl;
else
cout << "Error!\n";
}
return 0;
}