atoi和itoa函数

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int Myatoi(const char* str)
{
	if(str==NULL)//判断指针是否为空
	{
		printf("Pointer is NULL\0");
		return 0;
	}
	while(*str==' ')//忽略前导空字符
		str++;
	int sign=1;//判断符号
	if(*str=='-')
		sign=-1;
	if(*str=='+' || *str=='-')
		++str;//str指向符号位的后一位
	int integer=0;
	while(*str<='9' && *str>='0'){
		integer=integer*10+*str-'0';
		++str;
	}
	return integer*sign;
}

char* Myitoa(int value,char *str)
{
	if(value==0)
		return "0";
	int sign=1;
	char *tempStr=str;
	if(value<0){
		str[0]='-';//第一个是负号
		value=-value;
		sign=-1;
		str++;
	}
	while(value!=0){
		*str=value%10+'0';
		value=value/10;
		++str;
	}
	*str='\0';
	if(sign==1){//无符号时从0开始逆序
		int i=0;
		int j=strlen(tempStr)-1;//注意此处下标的值
		while(i<j)
		{
			char temp;
			temp=tempStr[i];
			tempStr[i]=tempStr[j];
			tempStr[j]=temp;
			i++;
			j--;
		}
	}else{

		int i=1;//有符号时从1开始逆序
		int j=strlen(tempStr)-1;//注意此处下标的值
		while(i<j)
		{
			char temp;
			temp=tempStr[i];
			tempStr[i]=tempStr[j];
			tempStr[j]=temp;
			i++;
			j--;
		}	

	}
	return tempStr;
}
int main()
{
	int value=-10;
	char str[100];
	printf("%s\n",Myitoa(value,str));
	return 0;
}

时间: 2024-08-26 22:24:27

atoi和itoa函数的相关文章

c++实现atoi()和itoa()函数(字符串和整数转化)

  c++实现atoi()和itoa()函数(字符串和整数转化) 一:起因 (1)字符串类型转化为整数型(Integer),还是字符串类型(String)转化为Double类型,这在java里面有非常好的内部函数,很easy的事情: (2)但是在c里面没有Integer Double等包装类,由char[]数组转化为整数型就变得不那么简单了,atoi()  itoa()在widows下面有,但是网上说linux 下好像没有 itoa() 函数,用 sprintf() 好了,但是本人测试了一下sp

【练习题】atoi和itoa函数的实现

int atoi (const char * str); //Convert string to integer char * itoa ( int value, char * str, int base ); //Convert integer to string (non-standard function) #include <stdio.h> #include <ctype.h> int my_atoi(const char *s) { int i =0; int tota

C语言itoa()函数和atoi()函数详解(整数转字符C实现)

C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串. 1.int/float to string/array: C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串,下面列举了各函数的方法及其说明.● itoa():将整型值转换为字符串.● ltoa():将长整型值转换为字符串.● ultoa():将无符号长整型值转换为字符串.● gcvt():将浮点型数转换为字符串,取四舍五入.● ecvt():将双精度浮点型值转换为字符串

C语言itoa函数和atoi 函数

C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串.以下是用itoa()函数将整数转 换为字符串的一个例子:  # include <stdio.h>  # include <stdlib.h>  void main (void)  {  int num = 100;  char str[25];  itoa(num, str, 10);  printf("The number 'num' is %d and the string 's

C语言itoa()函数和atoi()函数详解(整数转字符)

http://c.biancheng.net/cpp/html/792.html C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串. 以下是用itoa()函数将整数转换为字符串的一个例子: # include <stdio.h> # include <stdlib.h> void main (void) { int num = 100; char str[25]; itoa(num, str, 10); printf("The num

itoa()函数和atoi()函数详解

C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串. 以下是用itoa()函数将整数转换为字符串的一个例子:# include <stdio.h># include <stdlib.h>void main (void){int num = 100;char str[25];itoa(num, str, 10);printf("The number 'num' is %d and the string 'str' is %s. \n&qu

用itoa()函数将整数转换为字符串

在C语言中提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串.以下是用itoa()函数将整数转换为字符串的一个例子:atoi     把字符串转换成整型数itoa     把一整数转换为字符串 下面分享一下应用范例,可学习0基础C语言开发教程: #include "stdio.h" #include "ctype.h" #include "stdlib.h" /* Converts a character strin

atoi和itoa

itoa()函数的原型为: char *itoa( int value, char *string,int radix);itoa()函数有3个参数:第一个参数是要转换的数字,第二个参数是要写入转换结果的目标字符串,第三个参数是转换数字时所用的基数.在例中,转换基数为10.10:十进制:2:二进制...itoa并不是一个标准的C函数,它是Windows特有的,如果要写跨平台的程序,请用sprintf. 是Windows平台下扩展的,标准库中有sprintf,功能比这个更强,用法跟printf类似

atoi、itoa,strcpy,strcmp,memcpy等实现

原文:http://www.cnblogs.com/lpshou/archive/2012/06/05/2536799.html 1.memcpy.memmove.memset源码 link:http://note.youdao.com/share/?id=1f826e4337c7db272e94fdb4f267a8de&type=note 2.strcpy.strcat等源码 link:http://note.youdao.com/share/?id=d23a598b2e31321517ed5