《c程序设计语言》读书笔记-字符型0-9转为数字0-9

时间:2023-11-11 13:09:32
#include <stdio.h>

#define Num 10

int atoi(char s[]);

int main()
{
int c,i = 0;
char s[Num];
int result; while((c = getchar()) != EOF && c != '\n' && i < Num)
{
s[i] = c;
i++;
}
result = atoi(s);
printf("%d\n",result); return 0;
}
int atoi(char s[])
{
int i,n = 0; for(i = 0;s[i] >= '0' && s[i] <= '9';i++)
{
n = 10*n + (s[i] - '0');
} return n;
}