c語言問題字串轉數值

2009-04-08 8:25 am
the procedure of transferring ASCII string to integer
這段話我看不懂
更不會寫程式
要用C語言
不能call function---> aoti

回答 (1)

2009-04-08 1:07 pm
✔ 最佳答案
int my_atoi(char *s)
{
int i, n;

n = 0;
for (i=0; s[i] >= '0' && s[i] <= '9'; i++) {
n = 10 * n + (s[i] - '0');
}
return n;
}

int main()
{
char str[256];

printf("Please enter an positive integer: ");
scanf("%s",str);

printf("The string has been converted to an int %d\n", my_atoi(str));
}

// The function my_atoi() is what you are looking for.
// s[i] - '0' is the difference in integer between a digit with the character 0
// This function works a string from left to right. Every time the loop
// will multiple the sum by 10.



收錄日期: 2021-04-29 22:18:15
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20090408000016KK00278

檢視 Wayback Machine 備份