Friday, June 27, 2008

Source code for converting a string to an integer value.

int StrToInt( char str[]) {

bool bFlagNeative =false;
if( str[0] == '-') {

bFlagNeative =true;
}
int i=0;
int num =0;
while(str[i] != '\0') {

num *=10;
num+= (str[i++] - '0');
}
if(bFlagNeative)
num *= -1;
return num;

}

No comments: