https://programmers.co.kr/learn/courses/30/lessons/72410
나의 코드
#include <string>
#include <vector>
#include <cctype>
#include <iostream>
using namespace std;
string solution(string new_id) {
string answer = "";
for (int i = 0; i < new_id.size(); i++) { // 1,2,3,4
if (new_id[i] >= 'A' && new_id[i] <= 'Z') {
answer += new_id[i] + 32;
}
else if (new_id[i] != '-' && new_id[i] != '_' && new_id[i] != '.' && (new_id[i] < 'a' || new_id[i] > 'z') && !isdigit(new_id[i])) {
continue;
}
else {
if (new_id[i] == '.') {
if (answer.empty()|| answer[answer.size()-1] == '.') {
continue;
}
}
answer += new_id[i];
}
}
if (answer.size() > 0 && answer[answer.size() - 1] == '.') { //4
answer = answer.substr(0, answer.size() - 1);
}
if (answer.empty()) answer = "a"; //5
else if (answer.size() >= 16) { //6
answer = answer.substr(0, 15);
if (answer[answer.size() - 1] == '.') { //6
answer = answer.substr(0, answer.size() - 1);
}
}
if (answer.size() <= 2) { //7
char add_c = answer[answer.size() - 1];
while (answer.size() < 3) {
answer += add_c;
}
}
return answer;
}
c++에 있는 cctype 라이브러리를 사용하니 숫자잡는 것은 금방이었다.
처음에는 .의 개수를 세어 . 을 처리하려했으나 . 과 . 사이의 문자가 모두 제외되는 경우가 있기 때문에 위 코드와 같이 처리했다.
'전공공부 > 코딩테스트' 카테고리의 다른 글
(c++) 프로그래머스 오픈채팅방 (0) | 2022.01.04 |
---|---|
(c++) 프로그래머스 문자열 압축 (0) | 2022.01.04 |
(c++) 백준 1655번 가운데를 말해요 (0) | 2021.12.29 |
(c++) 백준 12865 평범한 배낭 (0) | 2021.12.29 |
(c++) 프로그래머스 해시 위장 (0) | 2021.12.11 |