字符串转整数
字符串转整数,不支持负号
int atoi(char s[])
{
int n = 0;
for (int i = 0; s[i] >= '0' && s[i] <= '9'; i++)
n = 10*n + (s[i] - '0');
return n;
}
整数转字符串十进制,不支持负号
void itoa2(char s[],int n)
{
int i,tmp;
for(i=0; n/10 != 0; n /= 10)
s[i++] = n%10 + '0';
s[i] = n + '0';
s[i+1] = ' ';
// reverse
for(int j=0; j<i; ++j) {
tmp = s[i];
s[i--] = s[j];
s[j] = tmp;
}
}
整数转字符串十进制,支持负号
void itoa(char s[],int n)
{
int i = 0,j=0,tmp;
if(n < 0) {
n = -n;
s[i++] = '-';
j = 1;
}
for(; n/10 != 0; n /= 10)
s[i++] = n%10 + '0';
s[i] = n + '0';
s[i+1] = ' ';
for( ; j<i; ++j) {
tmp = s[i];
s[i--] = s[j];
s[j] = tmp;
}
}