Coding Test/Programmers
[프로그래머스] Lv. 1: 문자열을 정수로 바꾸기 - C, C++, Java
찬 주
2025. 1. 20. 20:53
문제
https://school.programmers.co.kr/learn/courses/30/lessons/12925
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
접근법
각 언어마다 라이브러리 사용하면 된다.
코드
C
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
// 파라미터로 주어지는 문자열은 const로 주어집니다. 변경하려면 문자열을 복사해서 사용하세요.
int solution(const char* s) {
return atoi(s);
}
<stdlib.h> 헤더에 선언되어 있는 atoi() 함수는 문자열을 정수로 변환해 반환한다.
ASCII to INT 라는 뜻이다. C++에서도 사용할 수 있다.(하지만 굳이?)
C++
#include <string>
#include <vector>
using namespace std;
int solution(string s) {
return stoi(s);
}
<string> 헤더에 선언되어 있는 stoi() 함수는 문자열을 정수로 변환해 반환한다.
String to INT라는 뜻이다.
Java
class Solution {
public int solution(String s) {
return Integer.parseInt(s);
}
}
Integer 클래스의 static 함수인 parseInt()는 문자열을 정수로 변환해 반환한다.