티스토리 뷰

반응형

신입사원 네오의 업무를 도와주기 위해 

새로운 카카오 아이디를 만드려는 고객들이 규정에 맞지 않는 아이디를 입력한다면

규정에 맞는 새로운 아이디를 추천해주는 프로그램을 개발해야 하는 문제

 


문제의 조건

 

1. 고객이 입력한 아이디를 담은 string 배열 new_id가 주어짐

 

2. 아이디 변환 규칙

 

 

위 순서대로 차례차례 구현만 하면 된다. 

 


#include <string>
#include <vector>
#include <algorithm>

using namespace std;

string solution(string new_id) {
    string answer = new_id;
    int stage = 1;
    
    //5단계부터는 new_id가 빈 문자열이 될 수도 있기 때문에 처음 4단계까지만 반복문 속에서 반복 
    while (4 >= stage)
    {
        for (int i = 0; answer.length() > i; i++)
        {
            switch (stage)
            {
                case 1:
                    if ('A' <= answer[i] && 'Z' >= answer[i])
                        answer[i] = answer[i] - 'A' + 'a';
                    break;
                case 2:
                    if ('a' <= answer[i] && 'z' >= answer[i]) 
                        continue;
                    else if ('0' <= answer[i] && '9' >= answer[i])
                        continue;
                    else if ('-' == answer[i])
                        continue;
                    else if ('_' == answer[i])
                        continue;
                    else if ('.' == answer[i])
                        continue;
                    else 
                    {
                        //문자열이 줄어들었기 때문에 i번째 인덱스에 다른 문자가 위치하게 됨
                        //바뀐 i번째 문자도 탐색해야 하기 때문에 문자 삭제 후 i 감소 시켜줌
                        auto it = answer.begin();
                        advance(it, i);
                        answer.erase(it);
                        i--;
                    }
                    break;
                case 3:
                    if (answer.length() - 1 == i)
                        continue;

                    if ('.' == answer[i] && '.' == answer[i+1])
                    {
                        auto it = answer.begin();
                        advance(it, i);
                        answer.erase(it);
                        i--;
                    }
                    break;
                case 4:
                    if (0 == i || answer.length() - 1 == i)
                    {
                        if ('.' == answer[i])
                        {
                            auto it = answer.begin();
                            advance(it, i);
                            answer.erase(it);
                        }
                    }
                    break;
            }
        }

        stage++;
    }

    //5단계부터는 따로 구현 
    if (answer.empty())
        answer += 'a';

    if (16 <= answer.length())
    {
        string tmp = "";
        for (int i = 0; 15 > i; i++)
            tmp += answer[i];

        if ('.' == tmp[14])
        {
            auto it = tmp.begin();
            advance(it, 14);
            tmp.erase(it);
        }

        answer = tmp;
    }

    while (3 > answer.length())
    {
        char last = answer.back();
        answer += last;
    }

    return answer;
}
반응형
댓글
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/02   »
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
글 보관함