코딩테스트/프로그래머스 - LV 0
[프로그래머스] 모음 제거
웅드
2024. 1. 4. 16:53
- 내 풀이
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
string solution(string my_string) {
string answer = "";
for(char& v : my_string){
if(v != 'a' && v != 'e' && v != 'i' && v != 'o' && v != 'u'){
answer += v;
}
}
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를 사용함
문자열에서 모음을 다 " "로 치환하는 것이다.
regex("[aeiou]+")는 정규표현식인데 + 가 붙은건 ""안 문자중에 1개만 있어도 치환하겟다는 것이다.
반응형