[프로그래머스] 숫자 문자열과 영단어 c++

semi·2021년 9월 5일
0

coding test

목록 보기
6/57

https://programmers.co.kr/learn/courses/30/lessons/81301

#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;

int solution(string s)
{
	map<string, int> m = { {"zero", 0}, {"one", 1}, {"two", 2},
	{"three", 3}, {"four", 4}, {"five", 5}, {"six", 6}, {"seven", 7}, {"eight", 8}, {"nine", 9} };

	int answer = 0;
	int i = 0;
	for (auto x : m)
	{
		while (1)
		{
			int loc = s.find(x.first);
			if (loc >= s.size())
				break;
			s.replace(s.begin() + loc, s.begin() + loc + x.first.size(), to_string(x.second));
		}
	}
	answer = stoi(s);
	return answer;
}

int main(void)
{
	string s1 = "one4seveneight", s2 = "23four5six7", s3 = "2three45sixseven", s4 = "123";
	cout << solution(s4);
	return 0;
}

0개의 댓글