自己实现atoi的功能
整理思路:

#include <stdio.h>
int len(const char *s)
{
int len = ;
while (s[len])
{
len++;
}
return len;
}
int myPower10(int len)
{ if (len == )
return ;
if (len == )
return ; int power = ;
for (int i = ; i < len; i++)
power *= ;
return power;
}
int myCToInt(char c)
{
c = c - '';
return c;
} int myAtoi(const char *s)
{
int i, volue = ,lens = len(s);
for (i = ; i < lens; i++)
{
volue += myCToInt(s[i]) * myPower10(lens - i - );
}
return volue;
} int main()
{
char a[] = "";
int volue = myAtoi(a);
printf("%d", volue);
return ;
}
运行:
3321
换一种思路呢:

自己实现atoi的功能的更多相关文章
- 输入一个表示整数的字符串,把该字符串转换成整数并输出(实现atoi函数功能)
例如输入字符串"345",则输出整数345.-----------------------------此题一点也不简单.不信,你就先不看一下的代码,你自己先写一份,然后再对比一下, ...
- [leetcode]_String to Integer (atoi)
非常考虑思维全面性的一道题,考验是否能够考虑本问题的方方面面. 题目:将一个string转换为int.实现函数atoi()的功能. 先应该明确atoi()有哪些特殊功能:(正常的正负数情况我就不列了) ...
- 转载 C++常用库函数atoi,itoa,strcpy,strcmp的实现
C++常用库函数atoi,itoa,strcpy,strcmp的实现 C语言字符串操作函数 1. 字符串反转 - strRev2. 字符串复制 - strcpy3. 字符串转化为整数 - atoi4. ...
- 【C语言】模拟实现atoi函数
atoi(表示 ascii to integer)是把字符串转换成整型数的一个函数. atoi()函数会扫描参数 nptr字符串,跳过前面的空白字符(例如空格,tab缩进等,可以通过isspace( ...
- 【Leet Code】String to Integer (atoi) ——常考类型题
String to Integer (atoi) Total Accepted: 15482 Total Submissions: 106043My Submissions Implement ato ...
- 数制转换itoa atoi int转字符串 字符串转int string转int int转string
在苦于昨晚最后一个数制转换题,他的转换结果必须是整形数,纳尼?转换完放数组里又要变成整形数.这是什么操作,而且如果是16进制,用字母A,B-表示,在进行运算时都难以计算. 突发奇想,当十进制成立的时候 ...
- c常用函数-atoi 和 itoa
atoi 和 itoa atoi的功能是把一个字符串转为整数 Action(){ int j; char *s=""; j = atoi(s); lr_output_message ...
- 转:C语言字符串操作函数 - strcpy、strcmp、strcat、反转、回文
转自:C语言字符串操作函数 - strcpy.strcmp.strcat.反转.回文 C++常用库函数atoi,itoa,strcpy,strcmp的实现 作者:jcsu C语言字符串操作函数 1. ...
- Java求职面试准备之常见算法
最近在求职面试,整理一下常见面试算法: 对TestAlgorithms.java中方法的测试见JunitTestAlgorithms.java(引入了junit4) 1.TestAlgorithms. ...
随机推荐
- 浅析tomcat nio 配置
[尊重原创文章摘自:http://blog.csdn.net/yaerfeng/article/details/7679740] tomcat的运行模式有3种.修改他们的运行模式.3种模式的运行是否成 ...
- iframe跨域cookie问题
今天在项目里面遇到了iframe跨域不能写cookie的问题.应用场景是这样的:有A和B两个业务,A要通过iframe的方式嵌入B,但是在ie下A不能通过写cookie的方式记录信息,在firefox ...
- (转)深入理解javascript连续赋值表达式
引入 今天逛园子的时候看到一道javascript面试题,是关于连续赋值的,正好最近读jQuery源码经常看到这种连续赋值的表达式,所以很感兴趣. 废话不多说,来看题: var a = {n: 1} ...
- java 中 ArrayList LinkedList Vector 三者的异同点
1.ArrayList和Vector都是基于数组实现的,所以查询速度很快,增加和删除(非最后一个节点)速度慢: Vector是线程安全的,ArrayList不是. 2.LinkedList 是一个双向 ...
- 在 .NET 4.0 中使用 .NET 4.5 中新增的特性(CallerMemberNameAttribute/CallerFilePathAttribute/CallerLineNumberAttribute)
介绍 标题中所说的三个特性 CallerMemberNameAttribute / CallerFilePathAttribute / CallerLineNumberAttribute 我们统称为调 ...
- C#中一种可调用的异常处理方法
之前做异常处理时,感觉很麻烦,每个地方都要写try和catch,在博客园上看到一篇文章http://www.cnblogs.com/artech/archive/2012/10/28/automati ...
- const 限定符
1.定义const对象 const限定符把一个对象转换成一个常量 const int Bufsize = 512; 定义Bufsize 为常量并初始化为512.变量Bufsize仍然是一个左值,但是不 ...
- 关于js中的for(var in)遍历属性报错问题
之前遇到过这个问题,但是没找到问题的所在,将for(var i in array){} 改成了for(var i ;i<array.length;i++)循环,但是今天又遇到了,mark一下错 ...
- gcc 使用 stdio.h
9876543210z@z:~/funnyC++$ cat main.cpp #include <stdio.h> int main() { ; ) { printf("%d&q ...
- Linux 下的dd命令使用详解(摘录)
一.dd命令的解释 dd:用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换. 注意:指定数字的地方若以下列字符结尾,则乘以相应的数字:b=512:c=1:k=1024:w=2 参数注释: 1. ...