날마다 새롭게 또 날마다 새롭게

atoi - 입력된 문자를 정수로 변환하는 함수 구현 코드 본문

프로그래밍/C / C++

atoi - 입력된 문자를 정수로 변환하는 함수 구현 코드

아무유 2013. 3. 13. 15:56

#include <ctype.h>

/* atoi : convert s to integer */

int atoi( char s[])

{   

int i, n, sign;

for(i=0;isspace(s[i]); i++)

;

sign = (s[i]=='-')?-1:1;

if(s[i]=='+' || s[i] == '-')

i++;

for(n=0;isdigit(s[i]);i++)

n=10*n+(s[i] - '0');

return sign * n;

}

Comments