面试:atoi和itoa的实现
1.int atoi(const char* src)
- nullptr指针
- 空白字符' ','\t','\n'
- 符号位
- 避免值溢出
- 出错信息保存在全局变脸errnum中
int errnum = ;
int atoi(const char* src){
if(src == nullptr) {
errnum = -; //empty string
return ;
} //remove whitespace characters.
while(*src == ' ' || *src == '\t' || *src == '\n'){
src++;
} int sign = ;
if(*src == '+'){
src++;
}else if(*src == '-'){
sign = -;
src++;
}
//only with sign bit
if(*src == '\0') {
errnum = -;
return ;
} long long res = ;
while(*src != '\0'){
if(*src >= '' && *src <= ''){
res = res * + *src - '';
if((sign == && res > 0x7fffffff) || (sign == - && (-*res) < (int)0x80000000)){
errnum = -; //out of range
return ;
}
}else{
errnum = -; //illegal character
return ;
}
src++;
}
return sign*res;
}
2. char* itoa(int val,char* buf,size_t radix)
- 指针有效性判断
- 符号位
- 基数(10,16,2进制)
- reverse操作
char* itoa(int val,char* buf,size_t radix){
assert(buf != nullptr);
char* p = buf;
if(val < ){
*p++ = '-';
val = - * val;
} int a = ;
do{
a = val % radix;
val /= radix;
if(a > ){
*p++ = char(a-+'a');
}else{
*p++ = (char)(a + '');
}
}while(val != ); *p = '\0';
//reverse
size_t len = strlen(buf);
int left = ;
if(*buf == '-' || *buf == '+'){
left++;
}
int right = len - ;
while(left < right){
swap(buf[left++],buf[right--]);
}
return buf;
}
面试:atoi和itoa的实现的更多相关文章
- 面试:atoi() 与 itoa()函数的内部实现(转)
原 面试:atoi() 与 itoa()函数的内部实现 2013年04月19日 12:05:56 王世晖 阅读数:918 #include <stdio.h> #include < ...
- atoi 和 itoa的实现
atoi 和 itoa是面试笔试经常要考到的题目,下面两份代码是用C语言实现的atoi和itoa: 1, atoi 原型: int atoi(const char *nptr); 函数说明: 参数np ...
- atoi 和 itoa
转自:http://www.cnblogs.com/cobbliu/archive/2012/08/25/2656176.html atoi 和 itoa是面试笔试经常要考到的题目,下面两份代码是用C ...
- atoi、itoa,strcpy,strcmp,memcpy等实现
原文:http://www.cnblogs.com/lpshou/archive/2012/06/05/2536799.html 1.memcpy.memmove.memset源码 link:http ...
- c常用函数-atoi 和 itoa
atoi 和 itoa atoi的功能是把一个字符串转为整数 Action(){ int j; char *s=""; j = atoi(s); lr_output_message ...
- 工作的准备:atoi,itoa,strcpy,memcpy,strcmp,二分查找,strcat
对常见的几个函数,周末没事写写,绝对是笔试面试中非频繁,前面n届学长无数次强调了,大家就别怀疑了.从今天开始,每天10道题. int atoi(const char* str) { if(str==N ...
- C函数的实现(strcpy,atoi,atof,itoa,reverse)
在笔试面试中经常会遇到让你实现C语言中的一些函数比如strcpy,atoi等 1. atoi 把字符串s转换成数字 int Atoi( char *s ) { int num = 0, i = 0; ...
- c++实现atoi()和itoa()函数(字符串和整数转化)
(0) c++类型所占的字节和表示范围 c 语言里 类型转换那些事儿(补码 反码) 应届生面试准备之道 最值得学习阅读的10个C语言开源项目代码 一:起因 (1)字符串类型转化为整数型(Integer ...
- [转载]C函数的实现(strcpy,atoi,atof,itoa,reverse)
在笔试面试中经常会遇到让你实现C语言中的一些函数比如strcpy,atoi等 1. atoi 把字符串s转换成数字 int Atoi( char *s ) { , i = ; ; ; isspace( ...
随机推荐
- android Activity中设置setResult 没有触发onActivityResult
昨天修改<manifest 文件中activity 的 模式为单例模式:android:launchMode="singleTask" ,发现我的onActivityResu ...
- 《mysql必知必会》学习_第13章_20180803_欢
第13章:分组过滤. P83 select count(*) as num_prods from products where vend_id=1003; #返回vend_id=1003的产品数目总值 ...
- forward请求转发,param参数传递以及request.getParameter图文讲解
<%@ page language="java" contentType="text/html;charset=utf-8" pageEncoding=& ...
- hdu 4920
http://acm.hdu.edu.cn/showproblem.php?pid=4920 给定两个n阶矩阵,求矩阵相乘后模3. 直接搞肯定会超时 特殊处理1和2的情况 实际上是水过的..... 貌 ...
- jdbc的配置(更新中)
MySQL的 JDBC URL 格式 for Connector/J 如下例: 格式如下: jdbc:mysql://[host][,failoverhost...][:port]/[databas ...
- AWS–Sysops notes
Monitoring, Metrics and Analysis 1.CouldWatch Introduction2.EC2 Status Troubleshooting3.Create A Cou ...
- 16位CRC校验_Delphi
unit Modbus_main; {$mode objfpc}{$H+} interface uses Classes, SysUtils, FileUtil, Forms, Controls, G ...
- 2.虚拟机安装的ubuntu全屏显示
虚拟机下面安装了ubuntu系统,显示的屏幕只有那么一小块儿,不知道如何才能全屏,那么如何全屏呢?且看下面经验. 方法/步骤 打开虚拟机,并点击要更改成全屏的那个ubuntu系统的电源 我的虚拟机名字 ...
- 【VB.NET】通过 IPIP.NET 数据库来查询IP地址
上一次介绍了利用纯真数据库查询IP地址详细信息的方法.然而纯真数据库是由网友反馈所提供的,很多数据描述并不准确,所以我上网找了一些其他的IP数据库,最后就找到了 ipip.net 这个网站所提供的IP ...
- 绿色版Mysql自动建立my.ini和命令行启动并动态指定datadir路径
1.先去下载绿色版的Mysql(https://cdn.mysql.com//archives/mysql-5.7/mysql-5.7.20-winx64.zip) 2.解压缩到任意目录(如D:\My ...