1. 程式人生 > >設計一個函數將一個數字字符串轉換為數字,如將”1024”轉換成1024輸出

設計一個函數將一個數字字符串轉換為數字,如將”1024”轉換成1024輸出

字符串 const while printf int main 轉換成 pri 一個數

#include <stdio.h>
int convert(char *str)
{
int k=0;
while(*str!=‘\0‘)
{
k=k*10+(*str++)-‘0‘;
}
return k;


}

int main(int argc, const char * argv[]) {
char *str="45";

int a=atoi(str);
printf("%d\n",a);


printf("%d\n",convert(str));
return 0;
}

設計一個函數將一個數字字符串轉換為數字,如將”1024”轉換成1024輸出