atoi()函数的实现
上一篇博客讲的是atoi()函数的功能及举例,现在呢,就自己写写代码(根据atoi()的功能)来表示atoi()函数的实现。我在这里先把atoi()函数的功能贴出来,也好有个参考啊~~~
atoi()函数的功能:将字符串转换成整型数;atoi()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负号才开始做转换,而再遇到非数字或字符串时('\0')才结束转化,并将结果返回(返回转换后的整型数)。
atoi()函数实现的代码:
/*
* name:xif
* coder:xifan@2010@yahoo.cn
* time:08.20.2012
* file_name:my_atoi.c
* function:int my_atoi(char* pstr)
*/int my_atoi(char* pstr)
{int Ret_Integer = 0;int Integer_sign = 1;/** 判断指针是否为空*/if(pstr == NULL){printf("Pointer is NULL\n");return 0;}/** 跳过前面的空格字符*/while(isspace(*pstr) == 0){pstr++;}/** 判断正负号* 如果是正号,指针指向下一个字符* 如果是符号,把符号标记为Integer_sign置-1,然后再把指针指向下一个字符*/if(*pstr == '-'){Integer_sign = -1;}if(*pstr == '-' || *pstr == '+'){pstr++;}/** 把数字字符串逐个转换成整数,并把最后转换好的整数赋给Ret_Integer*/while(*pstr >= '0' && *pstr <= '9'){Ret_Integer = Ret_Integer * 10 + *pstr - '0';pstr++;}Ret_Integer = Integer_sign * Ret_Integer;return Ret_Integer;
}
现在贴出运行my_atoi()的结果,定义的主函数为:int main ()
int main()
{char a[] = "-100";char b[] = "456";int c = 0;int my_atoi(char*); c = atoi(a) + atoi(b);printf("atoi(a)=%d\n",atoi(a));printf("atoi(b)=%d\n",atoi(b));printf("c = %d\n",c);return 0;
}
运行结果:
atoi()函数的实现相关推荐
- 编写实现atoi函数
题目:给定一个字符串,要求转成整数,不允许使用atoi或者其它类似库函数. 方案:实现这个函数并不难,但是要考虑以下几个问题 1.考虑字符串为空指针的时候 2.考虑字符串为空串 ...
- C语言atoi()函数用法
C语言atoi()函数用法 头文件:#include <stdlib.h> atoi() 函数用来将字符串转换成整数(int),其原型为: int atoi (const char * s ...
- atoi()函数定义
原型:int atoi (const char *nptr) 用法:#include <stdlib.h> 功能:将字符串转换成整型数:atoi()会扫描参数nptr字符串,跳过前 ...
- C语言atoi()函数(字符串转整数int类型)(atol()转换为long)
需包含头文件:C 标准库 - <stdlib.h> 文章目录 描述 声明 参数 返回值 实例 在windows VS上测试 20211206 用atoi转换long,发现溢出了 描述 C ...
- linux中atoi函数的实现 值得借鉴,【转】atoi()函数的实现
atoi()函数的功能:将字符串转换成整型数:atoi()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负号才开始做转换,而再遇到非数字或字符串时('\0')才结束转化,并将结果返回( ...
- C语言中atoi()函数的用法
C语言中atoi()函数的用法 一.学习要点: 1.int atoi(const char *str)把参数str所指向的字符串转换为一个整数: 二.代码 #include<stdio.h> ...
- Linux下main函数带参数问题和atoi函数详解
1.main函数带参数 基本格式:main(int argc,char * argv[]) argc参数保存命令行参数的个数,是整形变量 具体用法可有如下代码看下: a1.c: #include &l ...
- atoi函数_吊打面试官 | 腾讯经典考点写代码实现atoi函数
点击蓝字关注我哦 以下是本期干货视频视频后还附有文字版本哦 ▼<腾讯经典考点-写代码实现atoi函数>▼ ps:请在WiFi环境下打开,如果有钱任性请随意 在腾讯面试时,经常会被问到如何用 ...
- C语言atoi()函数:将字符串转换成int(整数)和sprintf和memset
atoi()函数 #include <stdio.h> #include <stdlib.h>int main () {int i;char buffer[256];print ...
- C atoi函数
作用 atoi()函数将数字格式的字符串转换为整数类型.例如,将字符串1253124127转换成数字1253124127. 注意 首要注意 atoi函数可以转化如下这种字符串 1fdafhdjfhka ...
最新文章
- php 接口curl,php中接口强大之处php_curl
- CSP认证	201312-1出现次数最多的数[C++题解]:简单题
- 电信用户流失预测案例(2)(特征工程)
- uploadify的使用错误
- CloudFoundry cf push命令报错误码223的解决方法
- python es 数据库 ik_Linux系统:centos7下搭建ElasticSearch中间件,常用接口演示
- 【HDU - 1172】猜数字 (枚举暴力)
- 报名通道开启 | 顶会 ICLR 2021:医疗对话生成与自动诊断国际竞赛,邀你来战!...
- 无法显示验证码去掉html,如何去除验证码-模版风格-易通免费企业网站系统 - Powered by CmsEasy...
- matlab2c使用c++实现matlab函数系列教程-atan函数
- UISearchBar--改变内部输入框的背景颜色
- MESOS集群高可用部署
- DSP28335定时器
- 汽车租赁系统V2.0
- 淘宝、天猫、京东、苏宁等平台抢购脚本
- 实验五 八段数码管显示(红绿灯)
- html5 在线设计,推荐十款非常优秀的 HTML5 在线设计工具
- 招投标概念及注意事项
- ajax浏览器崩溃,AJAX崩溃浏览器(200MB响应)
- 百度seo和谷歌seo的区别