首页 > 编程语言 > 详细

【C语言】数字的字符串转化为 数字

时间:2015-03-10 23:09:54      阅读:369      评论:0      收藏:0      [点我收藏+]
#include <stdio.h>


/*
这个字符串参数必须包含一个或者多个数字,函数应该把这些数字字符转换为整数并返回这个整数。
如果字符串参数包含了任何非数字字符,函数就返回零。

*/

int ascii_to_integer(char *str)
{
int i=0;
while(*str!=‘\0‘)
{
if(*str<‘0‘|| *str >‘9‘)
return 0;
i*=10;              //进位
i+=*str-‘0‘;
str++;
}
return i;
}

int main()
{
char s[100]={0};
scanf("%s",s);
printf("%d\n",ascii_to_integer(s));


return 0;
}

【C语言】数字的字符串转化为 数字

原文:http://blog.csdn.net/a781558066/article/details/44183025

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!