atoi

leetcode——String to Integer (atoi)

无人久伴 提交于 2020-01-10 03:24:25
leetcode——String to Integer (atoi) 【题目描述】 Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace characters

atoi and leading 0's for decimal numbers

泄露秘密 提交于 2020-01-07 04:19:13
问题 When using atoi in C I am trying to convert a char array of numbers to an int . I have leading 0 's on my number though and they are not preserved when I print the number out later. char num[] = "00905607"; int number; number = atoi(num); printf("%d", number); The output from this will be 905607 and I would like it to be 00905607 . Any ideas? 回答1: Use strtol instead. It allows you to specify the base. 回答2: You can do padding on your printf() so if you wanted every output to be 8 characters

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

戏子无情 提交于 2020-01-06 15:40:49
题意 实现一个 atoi 函数,具体功能如下: 丢弃开头无用的空格,直到找到第一个非空格的字符为止。 当寻找到的第一个非空字符为正负号时或数字时,将其与后面尽可能多的连续数字组合起来形成整数。剩余的部分忽略。 如果第一个字符非上面三种情况时,则返回0。 如果数值超过了int的范围则返回INT_MAX或INT_MIN。 思路 直接模拟,注意细节就好了。时间复杂度 \(O(n)\) 。 代码 class Solution { public: int myAtoi(string str) { int len = str.size(), st = 0; for(int i = 0; i < len; ++i) if(str[i] != ' ') { st = i; break; } //cout << st << ' ' << str[st] << endl; bool flag = true; if(str[st] == '+') ++st; else if(str[st] == '-') { flag = false; ++st; } //cout << flag << endl; int res = 0; if(isdigit(str[st])) { while(st < len && isdigit(str[st])) { //cout << "res = " << res <<

C fgets strtok and atoi to read a line in C

五迷三道 提交于 2020-01-06 06:40:46
问题 #include <stdio.h> #include <string.h> int main(void) { char string[10000],*token; int garden[100],i=0; fgets(string,10000,stdin); token = strtok(string," "); while(strcmp(token,"\n") != 0){ garden[i] = atoi(token); i++; token = strtok(NULL," "); } return 0; } What is wrong with this code ? Why can't I read space separated integers from a line ? 回答1: Change while(strcmp(token,"\n") != 0){ to while(token != NULL){ 回答2: Your code only works when there's a space after the final number. Otherwise

C fgets strtok and atoi to read a line in C

好久不见. 提交于 2020-01-06 06:40:23
问题 #include <stdio.h> #include <string.h> int main(void) { char string[10000],*token; int garden[100],i=0; fgets(string,10000,stdin); token = strtok(string," "); while(strcmp(token,"\n") != 0){ garden[i] = atoi(token); i++; token = strtok(NULL," "); } return 0; } What is wrong with this code ? Why can't I read space separated integers from a line ? 回答1: Change while(strcmp(token,"\n") != 0){ to while(token != NULL){ 回答2: Your code only works when there's a space after the final number. Otherwise

use of atoi() function in c++

别等时光非礼了梦想. 提交于 2020-01-03 06:39:10
问题 when I pass a string variable in the below code, g++ gives an error: cannot convert ‘std::__cxx11::string {aka std::__cxx11::basic_string}’ to ‘const char*’ for argument ‘1’ to ‘int atoi(const char*)’ My code is: #include<iostream> #include<stdlib.h> using namespace std; int main() { string a = "10"; int b = atoi(a); cout<<b<<"\n"; return 0; } But if I change the code to : #include<iostream> #include<stdlib.h> using namespace std; int main() { char a[3] = "10"; int b = atoi(a); cout<<b<<"\n";

将字符转换成数字

随声附和 提交于 2019-12-26 17:34:24
Strcat,strcpy,strcmp,Strlen函数原型 http://blog.sina.com.cn/s/blog_66a61f310100i5fk.html atof():将字符串转换为 双精度浮点型 值。 atoi():将字符串转换为整型值。 atol():将字符串转换为长整型值。 strtod():将字符串转换为双精度浮点型值,并报告不能被转换的所有剩余数字。 strtol():将字符串转换为长整值,并报告不能被转换的所有剩余数字。 strtoul():将字符串转换为无符号长整型值,并报告不能被转换的所有剩余数字。 原型:extern int isspace(int c) #include <ctype.h> isspace函数,是一种计算机用语,主要用于检查参数c是否为空格字符。 说明:当c为空白符时,返回非零值,否则返回零。(空白符指空格、水平制表、垂直制表、换页、回车和换行符。) atoi函数原型: #include <stdio.h> #include <stdlib.h> int atoi(char *str) { int value = 0; while(*srt > '0' && *str < '9') { value = value*10 + (*str - '0'); str++ } return value; } 来源: https://www

C++ String to Integer Issue Using atoi(str.c_str())

江枫思渺然 提交于 2019-12-25 04:22:57
问题 I am trying to represent a variable in the form of a string to a integer, I have done so using; atoi(str.c_str()) The string is originally obtained from a text file and stored into a; CharArrayPtr cmemblock; Which is then represented as a string; string str; for(int i = 0; i < numberofvalues; i++) { str = cmemblock[i]; int number = atoi(str.c_str()); cout << number; } If I was to change the 'cout' to print str; str = cmemblock[i]; int number = atoi(str.c_str()); cout << str; The number show

leetcode 8: 字符串转整数(atoi)

£可爱£侵袭症+ 提交于 2019-12-23 23:42:55
实现 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' ,因为它的下一个字符不为数字。 示例 4: 输入: "words and 987" 输出: 0 解释: 第一个非空字符是 'w'

Using atoi with char

≯℡__Kan透↙ 提交于 2019-12-23 09:39:07
问题 Is there a way of converting a char into a string in C? I'm trying to do so like this: char *array; array[0] = '1'; int x = atoi(array); printf("%d",x); 回答1: How about: char arr[] = "X"; int x; arr[0] = '9'; x = atoi(arr); printf("%d",x); 回答2: char c = '1'; int x = c - '0'; printf("%d",x); 回答3: If you're trying to convert a numerical char to an int, just use character arithmetic to subtract the ASCII code: int x = myChar - '0'; printf("%d\n", x); 回答4: You need to allocate memory to the string