Life Engineering
[프로그래머스] 신규 아이디 추천 (C++) 본문
https://programmers.co.kr/learn/courses/30/lessons/72410
#include <string>
#include <vector>
#include <cctype>
using namespace std;
string solution(string new_id) {
string answer = "";
string answer1 = "";
for (int i=0; i<new_id.size(); i++){ //1,2 단계
if (('a'<=new_id[i] && 'z'>=new_id[i]) || ('0' <=new_id[i] && '9'>=new_id[i]) ||
new_id[i]=='-' || new_id[i]=='_' || new_id[i]=='.'){ //정상적이면 넣어주기
answer1+=new_id[i];
}
else if (isupper(new_id[i])){ //소문자 변환
answer1+=tolower(new_id[i]);
}
}
for (int i=0; i<answer1.size(); i++){ //3단계
answer+=answer1[i];
if (answer1[i]=='.'){
while (1){
if (i+1<=answer1.size()-1 && answer1[i+1]=='.'){ //. 2개 이상 있을 경우 넘기기
i++;
}
else{
break;
}
}
}
}
if (answer[0]=='.'){
answer.erase(answer.begin());
}
if (answer[answer.size()-1]=='.'){
answer.erase(answer.end()-1);
}
if (answer.size()==0){
answer+='a';
}
if (answer.size()>=16){
answer=answer.substr(0,15);
if (answer[answer.size()-1]=='.'){
answer.erase(answer.end()-1);
}
}
if (answer.size()<=2){
while (1){
answer+=answer.back();
if (answer.size()==3){
break;
}
}
}
return answer;
}
간단한 구현문제이나.. 많이 버벅인 점..
단계를 구분해서 코딩해주는게 중요한듯.. 안그러면 꼬이는 부분이 좀 생긴다
연속되는 마침표를 치환해줄때 이런 식으로 해준다.
'.' 가 나타날 경우, 일단 그걸 넣어주고 -> i를 증가시켜 i의 범위가 문자열 사이즈를 넘어서지 않고 증가시킨 i 번째 인덱스의 문자가 '.' 이면 계속해서 증가시켜준다. 탈출조건은 i 범위 넘거나 . 가 아닐 경우이다.
'Problem Solving' 카테고리의 다른 글
[BOJ 16236] 아기 상어 (C++) (0) | 2021.09.17 |
---|---|
[프로그래머스] 단체사진 찍기 (C++) (0) | 2021.09.16 |
[프로그래머스] 메뉴 리뉴얼 (C++) (0) | 2021.09.14 |
[프로그래머스] 카카오프렌즈 컬러링북 (C++) (0) | 2021.09.14 |
[프로그래머스] 합승 택시 요금 (C++) (0) | 2021.09.01 |