[프로그래머스/C++]Lv.0 - 모음 제거

YH J·2023년 4월 18일
0

프로그래머스

목록 보기
45/168

문제 링크

https://school.programmers.co.kr/learn/courses/30/lessons/120849

내 풀이

모음인 알파벳을 제외한 나머지 모든 알파벳을 answer에 +해준다.

내 코드

#include <string>
#include <vector>

using namespace std;

string solution(string my_string) {
    string answer = "";
    
    for(auto& s : my_string)
    {
        if(s != 'a' && s != 'e' && s != 'i' && s != 'o' && s != 'u')
            answer += s;
    }
    
    
    return answer;
}

다른 사람의 풀이

#include <string>
#include <vector>
#include <regex>

using namespace std;

string solution(string my_string) {
    string answer = "";
    answer = regex_replace(my_string, regex("[aeiou]+"), "");
    return answer;
}

다른 사람의 풀이 해석

regex를 사용하였다. my_string에서 모음을 다 ""로 치환하는 건데
regex("[aeiou]+")는 정규표현식인데 + 가 붙은건 aeiou중 1개만 있어도 치환하겠다는 것이다.
https://ansohxxn.github.io/cpp/chapter18-5/

profile
게임 개발자 지망생

0개의 댓글