转自:http://www.cnblogs.com/cobbliu/archive/2012/08/25/2656176.html

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 *itoaint 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的更多相关文章

  1. 面试:atoi() 与 itoa()函数的内部实现(转)

    原 面试:atoi() 与 itoa()函数的内部实现 2013年04月19日 12:05:56 王世晖 阅读数:918   #include <stdio.h> #include < ...

  2. atoi 和 itoa的实现

    atoi 和 itoa是面试笔试经常要考到的题目,下面两份代码是用C语言实现的atoi和itoa: 1, atoi 原型: int atoi(const char *nptr); 函数说明: 参数np ...

  3. atoi、itoa,strcpy,strcmp,memcpy等实现

    原文:http://www.cnblogs.com/lpshou/archive/2012/06/05/2536799.html 1.memcpy.memmove.memset源码 link:http ...

  4. c常用函数-atoi 和 itoa

    atoi 和 itoa atoi的功能是把一个字符串转为整数 Action(){ int j; char *s=""; j = atoi(s); lr_output_message ...

  5. 工作的准备:atoi,itoa,strcpy,memcpy,strcmp,二分查找,strcat

    对常见的几个函数,周末没事写写,绝对是笔试面试中非频繁,前面n届学长无数次强调了,大家就别怀疑了.从今天开始,每天10道题. int atoi(const char* str) { if(str==N ...

  6. C函数的实现(strcpy,atoi,atof,itoa,reverse)

    在笔试面试中经常会遇到让你实现C语言中的一些函数比如strcpy,atoi等 1. atoi 把字符串s转换成数字 int Atoi( char *s ) { int num = 0, i = 0; ...

  7. atoi 和itoa用法

    1.itoa 在linux下没有itoa这个函数 原型:char  *itoa(int   value,char   *string,int   radix)                   用法 ...

  8. c语言实现atoi和itoa函数。

    首先看atoi函数: C语言库函数名: atoi 功 能: 把字符串转换成整型数. 名字来源:ASCII to integer 的缩写. 原型: int atoi(const char *nptr); ...

  9. c++实现atoi()和itoa()函数(字符串和整数转化)

    (0) c++类型所占的字节和表示范围 c 语言里 类型转换那些事儿(补码 反码) 应届生面试准备之道 最值得学习阅读的10个C语言开源项目代码 一:起因 (1)字符串类型转化为整数型(Integer ...

随机推荐

  1. 让mbox支持up效果

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  2. 解决 PHP Fatal error: Call-time pass-by-reference has been removed

    PHP在升级到5.4版本的php可能会出现这种错误: 如果这样使用函数(或者类)的话,会产生一个 PHP Fatal error:foo(&$var);实际上,这样用法在php5.3中就会有提 ...

  3. mysql concat函数进行模糊查询

    concat() 函数,是用来连接字符串. 精确查询: select * from user where name=”zhangsan” 模糊查询: select * from user where ...

  4. dubbo用户指南

    用户指南 入门 背景 需求 架构 用法 快速启动 服务提供者 服务消费者 依赖 必需依赖 缺省依赖 可选依赖 成熟度 功能成熟度 策略成熟度 配置 Xml配置 属性配置 注解配置 API配置 示例 启 ...

  5. 更为详细的介绍Hadoop combiners-More about Hadoop combiners

    Hadoop combiners are a very powerful tool to speed up our computations. We already saw what a combin ...

  6. WCF项目中出现“目标程序集不包含服务类型”的解决办法

    如果创建新项目时(以下简称A项目)选择的是WCF相关的项目模板,并且在A项目中只定义接口而不实现接口,那么任何引用了A项目的项目,在调试时都会弹出警告框“目标程序集不包含服务类型.可能需要调整此程序集 ...

  7. python2 python3编码问题记录

    最近在服务器上跑脚本,linux自带的是python 2.x,中文显示经常有问题,通过下面两篇终于弄懂了. https://www.cnblogs.com/575dsj/p/7112767.html ...

  8. Spring定时器多定时任务配置

    spring-task.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&qu ...

  9. k-means聚类算法C++实现

    原文:http://www.cnblogs.com/luxiaoxun/archive/2013/05/09/3069594.html Clustering 中文翻译作“聚类”,简单地说就是把相似的东 ...

  10. (回溯法)数组中和为S的N个数

    Given a list of numbers, find the number of tuples of size N that add to S. for example in the list ...