C函数的实现(strcpy,atoi,atof,itoa,reverse)
在笔试面试中经常会遇到让你实现C语言中的一些函数比如strcpy,atoi等
1. atoi
把字符串s转换成数字
int Atoi( char *s )
{
int num = 0, i = 0;
int sign = 1; for( i=0; isspace(s[i]); i++ ); sign = (s[i] == '-')? -1:1; if( s[i] == '+' || s[i] == '-' )
i++; for( ;isdigit(s[i]); i++ )
{
num = 10*num + (s[i]-'0');
} return sign*num;
}
2. strcpy(char *src, char *dst)
把src复制到dst中
int Strcpy(char* src, char *dst)
{
// register char *tmp;
int i = 0;
while( src[i]!= NULL )
{
dst[i] = src[i++];
}
}
3.itoa( int n, char *s )
把数字转化成字符串
void Reverse( char *s )
{
int size = 0;
char tmp; while( s[size] != NULL )
size++;
size--; int i=0;
while( i <= size>>1 )
{
tmp = s[i], s[i] = s[size-i], s[size-i] = tmp;
i++;
}
} void ItoA( int n, char *s )
{
int sign = 1; if( n < 0 )
{
sign = -1;
n = -n;
} int i=0;
do
{
s[i++] = n%10 + '0';
} while((n/=10) > 0); if( sign == -1 )
s[i++] = '-';
s[i] = '\0';
Reverse( s );
}
4. atof( char * s )
把字符串转化成double类型
double AtoF( char *s )
{
int sign = 1;
int i = 0;
for( i=0; isspace(s[i]); i++ ); sign = (s[i] == '-')? -1:1; if( s[i] == '+' || s[i] == '-' )
i++; double num = 0.0;
double pow = 1.0;
//整数
for( ;isdigit(s[i]); i++ )
num = num*10 + (s[i]-'0'); for( i++; isdigit(s[i]); i++ )
{
num = num*10 + (s[i]-'0');
pow *= 10;
} return sign * (num/pow);
}
C函数的实现(strcpy,atoi,atof,itoa,reverse)的更多相关文章
- [转载]C函数的实现(strcpy,atoi,atof,itoa,reverse)
在笔试面试中经常会遇到让你实现C语言中的一些函数比如strcpy,atoi等 1. atoi 把字符串s转换成数字 int Atoi( char *s ) { , i = ; ; ; isspace( ...
- 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()函数(字符串和整数转化)
(0) c++类型所占的字节和表示范围 c 语言里 类型转换那些事儿(补码 反码) 应届生面试准备之道 最值得学习阅读的10个C语言开源项目代码 一:起因 (1)字符串类型转化为整数型(Integer ...
- 面试:atoi() 与 itoa()函数的内部实现(转)
原 面试:atoi() 与 itoa()函数的内部实现 2013年04月19日 12:05:56 王世晖 阅读数:918 #include <stdio.h> #include < ...
- atoi()和itoa()函数详解以及C语言实现
atoi()函数 atoi()原型: int atoi(const char *str ); 函数功能:把字符串转换成整型数. 参数str:要进行转换的字符串 返回值:每个函数返回 int 值,此值 ...
- c常用函数-atoi 和 itoa
atoi 和 itoa atoi的功能是把一个字符串转为整数 Action(){ int j; char *s=""; j = atoi(s); lr_output_message ...
- 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 ...
- _itoa _itow _itot atoi atof atol
函数原型: char *_itoa( int value, char *string, int radix ); //ANSI wchar_t * _itow( int value, wchar_t ...
随机推荐
- eclipse 集成maven插件
本文转载自:http://www.blogjava.net/fancydeepin/archive/2012/07/13/eclipse_maven3_plugin.html 环境准备: eclips ...
- [Regex Expression] Use Shorthand to Find Common Sets of Characters
In this lesson we'll learn shorthands for common character classes as well as their negated forms. v ...
- javascript之求最值
求最值: var selections = $("#deliveryGridSalesOrGoods").datagrid('getRows'); var costPrice = ...
- 常见HTTP状态码的含义
200 请求已成功,请求所希望的响应头或数据体将随此响应返回. 301 被请求的资源已永久移动到新位置. 302 请求的资源现在临时从不同的 URI 响应请求. 400 1.语义有误,当前请求无法被服 ...
- 文本输入框的两种div+css的写法
1.扁平化的设计风格.--淘宝 直接使用input.对其设置height.padding.使鼠标居中又不会占满输入框. 2.背景是图片的设计.--百度 试用span将input包裹 ...
- sql 根据时间获取数据
获取当月数据 MONTH(时间字段)=MONTH(GETDATE()) and year(时间字段)=year(GETDATE()) 计算两个时间差了多少分钟 DATEDIFF(mi,'7:00',c ...
- 《那些事之Log4j》什么是log4j?【专题一】
Log4j 简介 在应用程序中添加日志记录总的来说基于三个目的: 监视代码中变量的变化情况,周期性的记录到文件中供其他应用进行统计分析工作: 跟踪代码运行时轨迹,作为日后审计的依据: 担当集成开发环境 ...
- poj1094 topsort
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 32275 Accepted: 11 ...
- VB6基本数据库应用(四):数据的提取,新增和修改
列的第四篇,上一篇在:http://blog.csdn.net/jiluoxingren/article/details/9474661 数据的提取,新增和修改 由于在写第三章的时候没有充分考虑这一章 ...
- 【笔记】Unix 平台标准
POSIX 表示可移植操作系统接口(Portable Operating System Interface ,缩写为 POSIX ),POSIX标准定义了操作系统应该为应用程序提供的接口标准,是IEE ...