字符串转化为整数
字符串转化为整数:
代码:
#include <iostream>
#include <string.h>
using namespace std;
int fun(char *str){//实现字符串转换为整数
int d, num=0, len;
while(*str != '\0'){
d = *str-'0';//字符串转换为数字
len = strlen(str)-1;
for(int i = 0; i < len; i++){
d = d*10;//取出字符串各个位上对应的数字
}
num += d;
len--;
str++;
}
return num;
}
int main(){
char s[]={"123456789"};
cout << fun(s);
return 0;
}
结果:
还没有评论,来说两句吧...