题目说明:

1、设计函数: int atoi(const char *nptr);

2、功能:把字符串转换成整型数,atoi()会扫描参数nptr字符串,如果第一个非空格字符存在,

是数字或者正负号则开始做类型转换,之后检测到非数字(包括结束符 \0) 字符时停止转换,

返回整型数。否则,返回零,

3、头文件: #include <stdlib.h>

程序一:

#include <stdio.h>

#include <stdlib.h>   //isspace()函数的头文件

int myAtoi(const char* s)

{

  int result = 0;

  int flag = 1;

  int i = 0;

  while(isspace(s[i]))

    i++;

  if(s[i] == '-')  //遇到负号

  {

    flag = -1;

    i++;

  }

  if(s[i] == '+')   //遇到正号

    i++;

  while(s[i] != '\0')

  {

    if((s[i] > '9') || (s[i] < '0'))

      break;

    int j = s[i] - '0';

    result = 10 * result + j;

    i++;

  }

  result = result * flag;

  return result;

}

int main()

{

  char* a = " -1234def";

  char* b = "+5678";

  int i = myAtoi(a);

  int j = myAtoi(b);

  printf("%d \n",i);

  printf("%d \n",j);

  return 0;

}

程序二:

#include <cctype>
int my_atoi(const char* p){
  assert(p != NULL);
  bool neg_flag = false;// 符号标记
  int res = 0;          // 结果
  if(p[0] == '+' || p[0] == '-')
    neg_flag = (*p++ != '+');
  while(isdigit(*p)) res = res*10 + (*p++ - '0');
  return neg_flag ?0 -res : res;
}

字符串转换成整型数 atoi()的更多相关文章

  1. 笔试面试记录-字符串转换成整型数等(aatoi,itoa)

    C语言中经常用到字符串与数字之间的相互转换,常见的此类库函数有atof(字符串转换成浮点数).atoi(字符串转换成整型数).atol(字符串转换成长整形).itoa(整型数转换成字符串).ltoa( ...

  2. 字符串转换成整型,到底使用int.Parse,Convert.ToInt32还是int.TryParse?

    当我们想把一个字符串转换成整型int的时候,我们可能会想到如下三种方式:int.Parse,Convert.ToInt32和int.TryParse.到底使用哪种方式呢? 先来考虑string的可能性 ...

  3. python将字符串转换成整型

    将字符串转换成,整型,从字面理解很容易让人误会. 比如,要把这个"abcabc"转换成整型,臣妾做不到啊.除成转成ascii. 我们所说字符串转成整型是这样的. s = " ...

  4. 算法八字符串转换正数(atoi)

    请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之 ...

  5. Java实现 LeetCode 8 字符串转换整数(atoi)

    8. 字符串转换整数 (atoi) 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们寻找到的第一个非 ...

  6. 17.把字符串转换成整数[atoi]

    [题目] 把字符串转换成整数,需要考虑字符串有效性. [代码]  C++ Code  123456789101112131415161718192021222324252627282930313233 ...

  7. VC++中如何将字符串转换成整型数字

    原文:http://blog.csdn.net/yongf2014/article/details/47071663 注意: atoi函数是c的函数,它的输入参数是char *类型. 你声明了stri ...

  8. 字符串转换atof atoi atol gcvt strtod strtol strto ul toascii tolower toupper

    atof(将字符串转换成浮点型数) 相关函数 atoi,atol,strtod,strtol,strtoul 表头文件 #include <stdlib.h> 定义函数 double at ...

  9. 【Linux C中文函数手册】 字符串转换函数

    字符串转换函数 1)atof 将字符串转换成浮点型数 相关函数 atoi,atol,strtod,strtol,strtoul表头文件 #include <stdlib.h>定义函数 do ...

随机推荐

  1. qsort,mergesort,插入排序

    //插入排序 int a[n]; ;i<=n;i++) { int s=a[i]; ; while(j&&a[j]>a[i]) { a[j+]=a[j]; j--; } a ...

  2. 【AR】增强现实安卓编程 - Vuforia SDK 的安装和使用 (Android Studio)

    Vuforia是个强大的AR平台.使用Vuforia API 可以实现物体识别,图片追踪,柱型追踪,多对象追踪,自定义目标追踪,云识别,文字识别,帧标识和虚拟按钮等功能. 它支持Android, iO ...

  3. 给jdk写注释系列之jdk1.6容器(6)-HashSet源码解析&Map迭代器

    今天的主角是HashSet,Set是什么东东,当然也是一种java容器了.      现在再看到Hash心底里有没有会心一笑呢,这里不再赘述hash的概念原理等一大堆东西了(不懂得需要先回去看下Has ...

  4. ORA-01034: ORACLE not available

    ora-01034:oracle   not   available    ora-27101:shared   mermory   realm   does   not   exist 解决办法: ...

  5. 练习题之Wait/Notify

    方案一: public class PrintABC { public static void main(String[] args) { AtomicInteger synObj = ); Runn ...

  6. Servlet & JSP - Cookie

    关于 Cookie 的内容,参考 HTTP - Cookie 机制 获取来自客户端的 cookie request.getCookies 方法可以获取来自 HTTP 请求的 cookie,返回的是 j ...

  7. MyBatis(3.2.3) - Configuring MyBatis using XML, typeAliases

    In the SQL Mapper configuration file, we need to give the fully qualified name of the JavaBeans for ...

  8. ubuntu忘记密码,忘记root密码的解决方法

    转载于http://forum.ubuntu.org.cn/viewtopic.php?t=272164 ubuntu的root默认是禁止使用的,在安装的时候也没要求你设置root的密码,和红帽系统系 ...

  9. c#中网络异常的处理办法

    加入try catch来判断,catch使用的WebException来处理 try { var request = WebRequest.Create(uri); using (var respon ...

  10. 让无线网卡同时工作在 AP 和 STA 模式

    这个帖子里的方法有点过时了,不推荐继续使用. 有的时候会碰到这么一种情况,带着电脑和手机出去蹭网,无奈只有一个账号,手机上了电脑就没得用了,电脑用了手机就上不了网.如果能用电脑连接 Wifi 然后再开 ...