-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
85 lines (73 loc) · 1.73 KB
/
Copy pathmain.cpp
File metadata and controls
85 lines (73 loc) · 1.73 KB
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
80
81
82
83
84
85
/*
* Variable Name Inverter
*
* Copyright © 2021 Howard C.
*
* Changes variable names from "var_" to "_var",
* assuming all variables with underscores are named this way
*/
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
inline bool isAlphabet(char c)
{
return ((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z'));
}
inline bool isNumber(char c)
{
return (c >= '0') && (c <= '9');
}
inline bool inVariable(char c)
{
return isAlphabet(c) || isNumber(c) || (c == '_');
}
int main()
{
ifstream ifs;
ofstream ofs;
string file;
cin >> file;
ifs.open(file);
if (!ifs.is_open()) {
cout << "Cannot open file." << endl;
return 0;
}
ofs.open("out-" + file);
string buf;
char c;
while ((c = ifs.get()) != EOF) {
if ((c == ' ') || (c == '\n')) {
ofs << buf << c;
buf.clear();
} else if (c == '_') {
char c2 = ifs.peek();
if (inVariable(c2)) { // variable name not finished
buf += c;
continue;
}
int idx;
for (idx = buf.size() - 1; idx >= 0; idx--) {
if (!inVariable(buf[idx])) {
idx++;
break;
}
}
if (idx == -1) {
idx = 0;
}
if (isNumber(buf[idx])) {
cout << "I think something's wrong here..." << endl;
return 0;
}
ofs << buf.substr(0, idx) << '_' << buf.substr(idx, buf.size() - idx);
buf.clear();
} else {
buf += c;
}
}
ofs << buf;
ifs.close();
ofs.close();
return 0;
}