atoi

atoi implementation in C++

China☆狼群 提交于 2019-12-23 04:43:29
问题 Given this implementation of atoi in C++ // A simple atoi() function int myAtoi(char *str) { int res = 0; // Initialize result // Iterate through all characters of input string and update result for (int i = 0; str[i] != '\0'; ++i) res = res*10 + str[i] - '0'; // return result. return res; } // Driver program to test above function int main() { char str[] = "89789"; int val = myAtoi(str); printf ("%d ", val); return 0; } How exactly does the line res = res*10 + str[i] - '0'; Change a string

leetcode 8. 字符串转换整数 (atoi)

限于喜欢 提交于 2019-12-23 01:48:58
请你来实现一个 atoi 函数,使其能将字符串转换成整数。 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止。 当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之后面尽可能多的连续数字组合起来,作为该整数的正负号;假如第一个非空字符是数字,则直接将其与之后连续的数字字符组合起来,形成整数。 该字符串除了有效的整数部分之后也可能会存在多余的字符,这些字符可以被忽略,它们对于函数不应该造成影响。 注意:假如该字符串中的第一个非空格字符不是一个有效整数字符、字符串为空或字符串仅包含空白字符时,则你的函数不需要进行转换。 在任何情况下,若函数不能进行有效的转换时,请返回 0。 说明: 假设我们的环境只能存储 32 位大小的有符号整数,那么其数值范围为 [−2 31 , 2 31 − 1]。如果数值超过这个范围,请返回 INT_MAX (2 31 − 1) 或 INT_MIN (−2 31 ) 。 示例1: 输入 : "42" 输出 : 42 示例2: 输入 : " -42" 输出 : - 42 解释 : 第一个非空白字符为 '-' , 它是一个负号。 我们尽可能将负号与后面所有连续出现的数字组合起来,最后得到 - 42 。 示例3: 输入 : "4193 with words" 输出 : 4193 解释 : 转换截止于数字 '3'

字符串转换整数 (atoi)(leetcode)

落爺英雄遲暮 提交于 2019-12-21 09:35:24
题目描述如下: 请你来实现一个 atoi 函数,使其能将字符串转换成整数。 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止。 当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之后面尽可能多的连续数字组合起来,作为该整数的正负号;假如第一个非空字符是数字,则直接将其与之后连续的数字字符组合起来,形成整数。 该字符串除了有效的整数部分之后也可能会存在多余的字符,这些字符可以被忽略,它们对于函数不应该造成影响。 注意:假如该字符串中的第一个非空格字符不是一个有效整数字符、字符串为空或字符串仅包含空白字符时,则你的函数不需要进行转换。 在任何情况下,若函数不能进行有效的转换时,请返回 0。 说明: 假设我们的环境只能存储 32 位大小的有符号整数,那么其数值范围为 [−2 31 , 2 31 − 1]。如果数值超过这个范围,qing返回 INT_MAX (2 31 − 1) 或 INT_MIN (−2 31 ) 。 示例 1: 输入: "42" 输出: 42 示例 2: 输入: " -42" 输出: -42 解释: 第一个非空白字符为 '-', 它是一个负号。 我们尽可能将负号与后面所有连续出现的数字组合起来,最后得到 -42 。 示例 3: 输入: "4193 with words" 输出: 4193 解释: 转换截止于数字 '3'

17、字符串转换整数 (atoi)

岁酱吖の 提交于 2019-12-21 08:12:11
17、字符串转换整数 (atoi) 请你来实现一个 atoi 函数,使其能将字符串转换成整数。 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止。 当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之后面尽可能多的连续数字组合起来,作为该整数的正负号;假如第一个非空字符是数字,则直接将其与之后连续的数字字符组合起来,形成整数。 该字符串除了有效的整数部分之后也可能会存在多余的字符,这些字符可以被忽略,它们对于函数不应该造成影响。 注意:假如该字符串中的第一个非空格字符不是一个有效整数字符、字符串为空或字符串仅包含空白字符时,则你的函数不需要进行转换。 在任何情况下,若函数不能进行有效的转换时,请返回 0。 说明: 假设我们的环境只能存储 32 位大小的有符号整数,那么其数值范围为 [−231, 231 − 1]。如果数值超过这个范围,qing返回 INT_MAX (231 − 1) 或 INT_MIN (−231) 。 示例 1: 输入: "42" 输出: 42 示例 2: 输入: " -42" 输出: -42 解释: 第一个非空白字符为 '-', 它是一个负号。 我们尽可能将负号与后面所有连续出现的数字组合起来,最后得到 -42 。 示例 3: 输入: "4193 with words" 输出: 4193 解释: 转换截止于数字 '3'

Understanding an atoi() function

耗尽温柔 提交于 2019-12-21 04:27:23
问题 I'm a python programmer getting to learn C from the K&R book. This will seem like an awfully trivial question, but I'm stumped nonetheless. Attached below is a snippet of code from the K&R (RIP Ritchie!) book which implements the atoi() function. atoi(s) /*convert s to integer */ char s[]; { int i, n, sign; for (i=0; s[i]==' '||s[i] == '\n' || s[i] == '\t'; i++) ; /* skip whitespace */ sign = 1; if (s[i] == '+' || s[i] = '-') /* sign */ sign = (s[i++] == '+') ? 1 : -1; for (n=0; s[i] >= '0' &

atoi 和 itoa 还有sprintf的使用

蹲街弑〆低调 提交于 2019-12-21 01:57:56
目的是为了让字符数组中16进制数,以字符串的实现体现: int atoi(const char *nptr); int atoi(const char *nptr); atoi 把字符数据转换为int类型 char *itoa (int value, char *str, int base ); 返回值:返回指向str的指针,无错误返回。 [2] int value 被转换的整数,char *string 转换后储存的字符数组,int radix 转换进制数,如2,8,10,16 进制等,大小应在2-36之间 itoa 如何要在liunx中使用要添加源码程序,库里现在没有这个函数 int sprintf(char *string, char *format [,argument,…]); string-- 这是指向一个字符数组的指针,该数组存储了 C 字符串。 format-- 这是字符串,包含了要被写入到字符串 str 的文本。它可以包含嵌入的 format 标签,format 标签可被随后的附加参数中指定的值替换,并按需求进行格式化。format 标签属性是%[flags][width][.precision][length]specifier [argument]…:根据不同的 format 字符串,函数可能需要一系列的附加参数,每个参数包含了一个要被插入的值,替换了

How to implement atoi using SIMD?

人盡茶涼 提交于 2019-12-17 03:37:54
问题 I'd like to try writing an atoi implementation using SIMD instructions, to be included in RapidJSON (a C++ JSON reader/writer library). It currently has some SSE2 and SSE4.2 optimizations in other places. If it's a speed gain, multiple atoi results can be done in parallel. The strings are originally coming from a buffer of JSON data, so a multi-atoi function will have to do any required swizzling. The algorithm I came up with is the following: I can initialize a vector of length N in the

What is the difference between sscanf or atoi to convert a string to an integer?

北城余情 提交于 2019-12-17 02:58:08
问题 gcc 4.4.4 c89 What is better to convert a string to an integer value. I have tried 2 different methods atoi and sscanf. Both work as expected. char digits[3] = "34"; int device_num = 0; if(sscanf(digits, "%d", &device_num) == EOF) { fprintf(stderr, "WARNING: Incorrect value for device\n"); return FALSE; } or using atoi device_num = atoi(digits); I was thinking that the sscanf would be better as you can check for errors. However, atoi doesn't doing any checking. 回答1: You have 3 choices: atoi

8. 字符串转换整数 (atoi)

随声附和 提交于 2019-12-14 03:05:59
8. 字符串转换整数 (atoi) 难度 中等 描述 请你来实现一个 atoi 函数,使其能将字符串转换成整数。 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止。 当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之后面尽可能多的连续数字组合起来,作为该整数的正负号;假如第一个非空字符是数字,则直接将其与之后连续的数字字符组合起来,形成整数。 该字符串除了有效的整数部分之后也可能会存在多余的字符,这些字符可以被忽略,它们对于函数不应该造成影响。 注意:假如该字符串中的第一个非空格字符不是一个有效整数字符、字符串为空或字符串仅包含空白字符时,则你的函数不需要进行转换。 在任何情况下,若函数不能进行有效的转换时,请返回 0。 说明 假设我们的环境只能存储 32 位大小的有符号整数,那么其数值范围为 [−231, 231 − 1]。如果数值超过这个范围,请返回 INT_MAX (231 − 1) 或 INT_MIN (−231) 。 示例1 输入: "42" 输出: 42 示例2 输入: " -42" 输出: -42 解释: 第一个非空白字符为 '-', 它是一个负号。 我们尽可能将负号与后面所有连续出现的数字组合起来,最后得到 -42 。 示例3 输入: "4193 with words" 输出: 4193 解释: 转换截止于数字 '3'

Is atoi multithread safe? [closed]

感情迁移 提交于 2019-12-13 23:23:24
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 2 years ago . I am experiancing some error while creating a multithreaded program. While using gdb to debug, the atoi function is throwing error. Please help, is atoi multithread unsafe and if so, what are the alternatives? 回答1: Its quite easy to implement a replacement for atoi(): int strToInt(const char *text) { int n = 0,