본문 바로가기

코딩 테스트/프로그래머스 코딩테스트

신규 아이디 추천 - 미해결...

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

#include <regex> // 특정 문자 제거를 위한 라이브러리

using namespace std;

string solution(string new_id) {
    
    string answer = "";
    string str_lower;
    
    // 1 단계 : 대문자 -> 소문자
    for(int i=0; i<new_id.size(); i++)
    {
        if(new_id.at(i) >= 'A' && new_id.at(i) <= 'Z')
        {
            new_id.at(i) += 32;
        }
    }
    
    str_lower = new_id;
    string str_cpy = str_lower;
    
    printf("1 단계 결과 = %s\n", new_id.c_str());    
    
    // 2 단계 : 특정 문제 제외하고 제거
    
    int idx = 0;
    
    while(str_cpy[idx] != NULL)
    {
        // printf("Search...\n");
        char cCheck = str_cpy[idx];

        // 제외 문자 체크
        if((cCheck >= 'a' && cCheck <= 'z')  ||      // 알파벳 소문자
           (cCheck >= '0' && cCheck <= '9')     ||      // 숫자
           (cCheck == '-') ||                           // -
           (cCheck == '_') ||                           // _
           (cCheck == '.')                              // .
          )
        {
            // printf("유지 = %c ,%d\n", cCheck, idx);
            idx++;
        }
        else
        {
            // printf("제거 = %c ,%d\n", cCheck, idx);
            // str_cpy.erase(idx);
            str_cpy.erase(remove(str_cpy.begin(), str_cpy.end(), cCheck));
        }
    }

    printf("2 단계 결과 = %s\n", str_cpy.c_str());
    
    // 3 단계 : 마침표(.) 2 번 이상(..)이면 하나(.)로 치환

    string find_str = "..";
    string replace_str = ".";
    
    idx = 0;
    while(str_cpy[idx] != NULL)
    {
        if(idx >= 1)
        {
            if(str_cpy[idx-1] == '.' && str_cpy[idx] == '.')
            {
                str_cpy.replace(str_cpy.find(find_str), find_str.length(), replace_str);
                idx--;
            }
        }
        
        idx++;
    }
    
    printf("3 단계 결과 = %s\n", str_cpy.c_str());    
    
    // 4 단계 : 처음 혹은 마지막 문자가 마침표(.) 하나라면 제거
    
    if(str_cpy[0] == '.')
    {
        str_cpy.erase(0, 1);
    }
    
    if(str_cpy[str_cpy.length()-1] == '.')
    {
        str_cpy.erase(str_cpy.length()-1, 1);
    }
    

    printf("4 단계 결과 = %s\n", str_cpy.c_str());    
        
    // 5 단계 : 빈 문자열이라면 a를 대입
    
    if(str_cpy[0] == NULL)
    {
        // cout << "NULL!" << endl;
        str_cpy.append("a", 1);
    }
    

    printf("5 단계 결과 = %s\n", str_cpy.c_str());    
        
    // 6 단계 : 16자 이상이면 15자 제외한 나머지 문자 제거
    
    if(str_cpy.length() >= 16)
    {
        str_cpy.erase(15, str_cpy.length());
    }

        
    printf("6 단계 결과 = %s\n", str_cpy.c_str());    
        
    // 7 단계 : 길이가 2 이하라면, 마지막 문자를 길이가 3이 될 때까지 반복, 마지막이 온점(.)이라면 제거
    
    if(str_cpy.length() <= 2)
    {
        char last_char = str_cpy[str_cpy.length()-1];
        
        cout << "last_char = " << last_char << endl;
                
        while(str_cpy.length() < 3)
        {
            str_cpy += last_char;
            
            printf("마지막 문자 추가 중... = %s\n", str_cpy.c_str());    
        }
    }
    
    if(str_cpy[str_cpy.length() - 1] == '.')
    {
        str_cpy.erase(str_cpy.length()-1, 1);
    }
    
    
    printf("7 단계 결과 = %s\n", str_cpy.c_str());    
    
    answer = str_cpy;
    
    return answer;
}

'코딩 테스트 > 프로그래머스 코딩테스트' 카테고리의 다른 글

최솟값 만들기  (0) 2022.10.03
올바른 괄호  (0) 2022.10.03
행렬의 곱셈  (0) 2022.09.29
성격 유형 검사하기  (0) 2022.09.25
최댓값과 최솟값  (0) 2022.09.25