atoi 和 itoa的实现
atoi 和 itoa是面试笔试经常要考到的题目,下面两份代码是用C语言实现的atoi和itoa:
1, atoi
原型: int atoi(const char *nptr);
函数说明: 参数nptr字符串,如果第一个非空格字符不存在或者不是数字也不是正负号则返回零,否则开始做类型转换,之后检测到非数字(包括结束符 \0) 字符时停止转换,返回整型数。
#include <stdio.h>
#include <assert.h>
static int atoi(const char* str)
{
int result = 0;
int sign = 0;
assert(str != NULL);
// proc whitespace characters
while (*str==' ' || *str=='\t' || *str=='\n')
++str;
// proc sign character
if (*str=='-')
{
sign = 1;
++str;
}
else if (*str=='+')
{
++str;
}
// proc numbers
while (*str>='0' && *str<='9')
{
result = result*10 + *str - '0';
++str;
}
// return result
if (sign==1)
return -result;
else
return result;
}
2. itoa
char *itoa( int value, char *string,int radix);
原型说明:
value:欲转换的数据。
string:目标字符串的地址。
radix:转换后的进制数,可以是10进制、16进制等
char *itoa(int val, char *buf, unsigned radix)
{
char *p;
char *firstdig;
char temp;
unsigned digval;
p = buf;
if(val <0)
{
*p++ = '-';
val = (unsigned long)(-(long)val);
}
firstdig = p;
do{
digval = (unsigned)(val % radix);
val /= radix; if (digval > 9)
*p++ = (char)(digval - 10 + 'a');
else
*p++ = (char)(digval + '0');
}while(val > 0); *p-- = '\0 ';
do{
temp = *p;
*p = *firstdig;
*firstdig = temp;
--p;
++firstdig;
}while(firstdig < p);
return buf;
}
atoi 和 itoa的实现的更多相关文章
- 面试:atoi() 与 itoa()函数的内部实现(转)
原 面试:atoi() 与 itoa()函数的内部实现 2013年04月19日 12:05:56 王世晖 阅读数:918 #include <stdio.h> #include < ...
- 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; ...
- atoi 和itoa用法
1.itoa 在linux下没有itoa这个函数 原型:char *itoa(int value,char *string,int radix) 用法 ...
- c语言实现atoi和itoa函数。
首先看atoi函数: C语言库函数名: atoi 功 能: 把字符串转换成整型数. 名字来源:ASCII to integer 的缩写. 原型: int atoi(const char *nptr); ...
- c++实现atoi()和itoa()函数(字符串和整数转化)
(0) c++类型所占的字节和表示范围 c 语言里 类型转换那些事儿(补码 反码) 应届生面试准备之道 最值得学习阅读的10个C语言开源项目代码 一:起因 (1)字符串类型转化为整数型(Integer ...
随机推荐
- hdu 6380
#include<bits/stdc++.h> #define in(a) scanf("%d",&a) using namespace std; struct ...
- Oracle了解(一)
通常所说的Oracle数据库服务器由一个数据库和至少一个数据库实例组成. 数据库实例是由系统后台进程和分配的内存区域构成 实例你是提供服务的进程,数据库是存放的数据. 数据库是存储数据的文件 数据库实 ...
- vue 用checkbox 做多选,带选中样式
<dl v-for="(item,index) in listData" :key="index"> testName <label> ...
- mesg命令帮助文档(ubuntu 18.04)
MESG() User Commands MESG() NAME mesg - display (or do not display) messages from other users SYNOPS ...
- [笔记]New in Chrome 66
原文 CSS Typed Object Model 使用CSS object model,返回的一切都是字符串 el.style.opacity = 0.3; console.log(typeof e ...
- (一)python的前世今生
一:Python介绍 python的创始人为吉多·范罗苏姆(Guido van Rossum)(目前还活着),诞生于1989年,是一个脚本解释程序,由于python语言结构优美,清晰简单,随着人工智能 ...
- solr window环境安装配置和管理页面基本使用
solr介绍 来自官网http://lucene.apache.org/solr/解释: Solr is highly reliable, scalable and fault tolerant, p ...
- 利用PHP连接数据库操作用户注册、审核与登录页面
注册页面 <body ><h1>注册页面</h1><form action="zhucechuli.php" method="p ...
- Coursera, Machine Learning, notes
Basic theory (i) Supervised learning (parametric/non-parametric algorithms, support vector machine ...
- 缓存设计(cache-design)
分布式缓存设计 目前常见的缓存方案都是分层缓存,通常可以分为以下几层: 1.1NG本地缓存,命中的话直接返回 1.2 NG没有命中时则需要查询分布式缓存,如redis 1.3 如果分布式缓存没有命中则 ...