-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathJAVAC.cs
79 lines (73 loc) · 2.3 KB
/
JAVAC.cs
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
70
71
72
73
74
75
76
77
78
79
using System;
using System.Linq;
using System.Text;
// https://www.spoj.com/problems/JAVAC/ #ad-hoc #strings
// Turns Java-like identifiers into C++-like identifiers, and vice versa.
public static class JAVAC
{
public static string Solve(string identifier)
{
bool isJavaIdentifier = char.IsLower(identifier[0])
&& identifier.All(c => char.IsLetter(c));
// Just doing !isJavaIdentifier to save work here; technically the identifier could be both.
bool isCppIdentifier = !isJavaIdentifier
&& char.IsLower(identifier[0])
&& char.IsLower(identifier[identifier.Length - 1]);
for (int i = 1; i < identifier.Length - 1 && isCppIdentifier; ++i)
{
char l = identifier[i];
char r = identifier[i + 1];
// Either the character is lower, or it's an underscore with a word immediately after.
isCppIdentifier = char.IsLower(l) || (l == '_' && char.IsLower(r));
}
var output = new StringBuilder();
if (isJavaIdentifier)
{
for (int i = 0; i < identifier.Length; ++i)
{
char c = identifier[i];
if (char.IsLower(c))
{
output.Append(c);
}
else // It's an uppercase, so replace U with _u.
{
output.Append('_');
output.Append(char.ToLower(c));
}
}
}
else if (isCppIdentifier)
{
for (int i = 0; i < identifier.Length; ++i)
{
char c = identifier[i];
if (char.IsLower(c))
{
output.Append(c);
}
else // It's an underscore, so replace _u with U.
{
output.Append(char.ToUpper(identifier[++i]));
}
}
}
else
{
output.Append("Error!");
}
return output.ToString();
}
}
public static class Program
{
private static void Main()
{
string identifier;
while (!string.IsNullOrEmpty(identifier = Console.ReadLine()))
{
Console.WriteLine(
JAVAC.Solve(identifier));
}
}
}