Introduction to atoi and realization of atoi in C language

atoi

Let’s take a look at the official documents ( portal )

interface

int atoi (const char * str);

Convert string to integer

  • Parses the C-string str interpreting its content as an integral
    number, which is returned as a value of type int.
  • The function first discards as many whitespace characters (as in
    isspace) 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 base-10 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 in str is not a
    valid integral number, or if no such sequence exists because either
    str is empty or it contains only whitespace characters, no conversion
    is performed and zero is returned.

Parameters

  • C-string beginning with the representation of an integral number.

Return Value

  • On success, the function returns the converted integral number as an
    int value.
  • If the converted value would be out of the range of representable
    values by an int, it causes undefined behavior. See strtol for a more
    robust cross-platform alternative when this is a possibility.

Speaking of torture, use code verification to verify

int main() {
    
    
	//模拟实现atoi
	char str[] = "-22675";
	int ans = atoi(str);
	printf("%d\n",ans);
	system("pause");
	return 0;
}

Operation result: -22675

int main() {
    
    
	//模拟实现atoi
	char str[] = "   -22675";// 添加空格
	int ans = atoi(str);
	printf("%d\n",ans);
	system("pause");
	return 0;
}

Operation result: -22675

int main() {
    
    
	//模拟实现atoi
	char str[] = "   +22675";
	int ans = atoi(str);
	printf("%d\n",ans);
	system("pause");
	return 0;
}

Running result: 22675

int main() {
    
    
	//模拟实现atoi
	char str[] = " ww  +22675";
	int ans = atoi(str);
	printf("%d\n",ans);
	system("pause");
	return 0;
}

Operation result: 0

int main() {
    
    
	//模拟实现atoi
	char str[] = "   +22675wer";
	int ans = atoi(str);
	printf("%d\n",ans);
	system("pause");
	return 0;
}

Running result: 22675

After trying a lot of torture, everyone 1 should have discovered his function. Realize atoi by yourself

C language implementation

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<assert.h>
#include<windows.h>



int my_atoi(const char * str) {
    
    
	assert(str);
	int j = 0;
	while (str[j] != '\0') {
    
    
		// 将字符串前边的空格跳过
		while (str[j] != '\0' && str[j] == ' ') {
    
    
			j++;
		}
		//如果出现+  -  或者数字就结束循环,否则返回0
		if (str[j]!='+' && str[j] != '-' && (str[j]<'0' ||str[j]>'9')) {
    
    
			return 0;
		}
		break;	
	}
	
	// 程序走到这里说明开头是+123  或者-123类似开头
	int ans = 0;
	int tmp = 1;
	// 对 +  - 处理一下,利用tmp标记+  -
	if (str[j] == '+') {
    
    
		j++;
		
	}
	if (str[j] == '-') {
    
    
		tmp = -1;
		j++;
	}
	int count = 0;
	// 处理数字
	while (str[j] != '\0'&& str[j] >='0' &&str[j]<='9') {
    
    
		//printf("%d", pow(10, count));
		ans += pow(10,count)*(str[j] - '0');
		count++;
		j++;
	}
	int num = 0;
	// 反转一下
	while (ans) {
    
    
		num += pow(10, --count)*(ans % 10);
		ans = ans / 10;
	}
	return num*tmp;

}

int main() {
    
    
	//模拟实现atoi
	char str[] = "   +22675wer";
	int ans = my_atoi(str);
	printf("%d\n",ans);
	system("pause");
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45532227/article/details/111415517