int ascii_to_integer(char *str)函数 将一串数字字符转化成对应的整形数字

/*
int ascii_to_integer(char *str);
这个字符串参数必须包含一个或者多个数字,
函数应该把这些数字字符转换为整数并返回这个整数。
如果字符串参数包含了任何非数字字符,函数就返回零。请不必担心算数溢出。
*/


#include<stdio.h>

int ascii_to_integer(char *str)
{
	int res = 0;  //结果
	while (*str>='0' && *str<='9'&& *str != 0  )
	{
		res = res * 10 + (*str - '0');
		*str++;
	}
	return res;
}

int main(void)
{
	char *str_1 = "ss12p";//测试
	char *str_2 = "13245";//测试
	char *str_3 = "qwr";//测试 
	printf("%u\n", ascii_to_integer(str_1));
	printf("%u\n", ascii_to_integer(str_2));
	printf("%u\n", ascii_to_integer(str_3));

	return 0;
}


版权声明:本文为Irean_Lau原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。