不使用atoi将String转换为Integer

如果不使用给定参数的atoi,我如何将字符串转换为整数? 这是我尝试过的:

int main(int argc, char *argv[]){ for(int i = 1; i < argc; i++){ const char *p = argv[i]; int j = 0; while(isdigit(*p)){ j = j * 10 + *p - '0'; p++; printf("%d\n", j); } } } 

出于某种原因,它正在拆分并再次重新添加它们。

 $ ./a.out 55 6 50 66 5 55 6 5 50 6 66 

您正在打印所有中间结果。 保存打印输出,直到完成while循环。

 while(isdigit(*p)){ j = j * 10 + *p - '0'; p++; } printf("%d\n", j); 
 #include #include using namespace std; int main() { //char name[1000]; //cin>>name; char name[]="123456"; int data, result, output,Base; //volatile int Base; //int Base; data=1; result=0; Base=strlen(name); while (Base>= 1) { Base--; output=toascii(name[Base])-48; output=output*data; result= result+output; data=data*10; } std::cout<<"this is the string you enterd"<