개발 노트

int atoi(char* p) 함수를 구현하라.

무병장수권력자 2008. 3. 23. 21:31
int atoi(char* p) 함수를 구현하라.

작성자 : 김문규
최초 작성일 : 2008. 3 23

int convert_string_to_integer(char *_pchar)
{
   char *pchar = _pchar;

   int cnt = 0, value = 0, ispositive = 1;

   if(pchar == NULL) return 0;

   // check if the first character is '-' or not.
   if( *pchar == '-' )
   {
       ispositive = -1;
       pchar++;
   }

   while( (*pchar != '\0') && ('0' <= *pchar) && ('9' >= *pchar) )
   {
       value = value*10 + (int)*pchar-(int)'0';
       *pchar++;
   }
   return ispositive*value;
}