[프로그래머스] 신규 아이디 추천
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
string solution(string new_id) {
transform(new_id.begin(), new_id.end(), new_id.begin(), ::tolower);
string tmp = "";
for (int i = 0; i < new_id.length(); ++i) {
if ('0' <= new_id[i] && new_id[i] <= '9')
tmp += new_id[i];
else if ('a' <= new_id[i] && new_id[i] <= 'z')
tmp += new_id[i];
else if(new_id[i] == '.' || new_id[i] == '-' || new_id[i] == '_')
tmp += new_id[i];
}
new_id = tmp;
tmp = "";
bool period = false;
for (int i = 0; i < new_id.length(); ++i) {
if (new_id[i] == '.') {
if (!period) tmp += '.';
period = true;
}
else {
tmp += new_id[i];
period = false;
}
}
new_id = tmp;
if (new_id != "" && new_id[0] == '.') {
if(new_id.length() > 1) new_id = new_id.substr(1);
else new_id = "";
}
if (new_id != "" && new_id[new_id.length() - 1]=='.') {
if (new_id.length() > 1) new_id = new_id.substr(0, new_id.length() - 1);
else new_id = "";
}
if (new_id == "") new_id = "a";
if(new_id.length() > 15) new_id = new_id.substr(0, 15);
if (new_id != "" && new_id[new_id.length() - 1]== '.') {
if (new_id.length() > 1) new_id = new_id.substr(0, new_id.length() - 1);
else new_id = "";
}
while (new_id.length() < 3) {
new_id += new_id[new_id.length() - 1];
}
return new_id;
}
int main() {
solution("123_.def");
}