-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjavac.cpp
78 lines (76 loc) · 1.69 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
70
71
72
73
74
75
76
77
78
#include<cstdio>
#include<iostream>
#include<cctype>
#include<string>
using namespace std;
bool isC(char* str) {
if(str[0] <'a' || str[0] >'z') {
return false;
}
int i;
for(i=1;str[i];++i) {
if(str[i] == '_' && str[i-1] == '_') {
return false;
} else if((str[i] <'a' || str[i] >'z') && str[i] != '_') {
return false;
}
}
return str[i-1] >= 'a' && str[i-1] <= 'z';
}
string convertToC(char* str) {
if(str[0] < 'a' || str[0] >'z') {
return "";
}
string s("");
for(int i=0;str[i];++i) {
if(islower(str[i])) {
s += str[i];
} else if(isupper(str[i])) {
s += "_";
s += tolower(str[i]);
} else {
return "";
}
}
return s;
}
void convertToJava(char* str) {
string result = "";
bool capitalize = false;
int idx1 = 0;
int idx2 = 0;
for(int i=0;str[i];++i) {
if(str[i] == '_') {
capitalize = true;
idx2++;
} else {
if(capitalize) {
str[idx1] = toupper(str[idx2]);
capitalize = false;
} else {
str[idx1] = str[idx2];
}
idx1++;
idx2++;
}
}
str[idx1] = 0;
}
int main() {
char str[101];
while(scanf("%s", str) != EOF) {
string result;
if(isC(str)) {
convertToJava(str);
printf("%s\n", str);
continue;
} else {
string s = convertToC(str);
if(s == "") {
printf("Error!\n");
} else {
cout<<s<<'\n';
}
}
}
}