编写函数实现:整型数转换成字符串

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

void int_to_str(unsigned int src,unsigned char* dest);

void main(void)
{
	unsigned int num = 0;
	unsigned char str[11] = {0};
	printf("scanf a number:\n");
	scanf("%d",&num);
	int_to_str(num,str);	
	printf("after convert: %s\n",str);
}

void int_to_str(unsigned int src,unsigned char* dest)
{
	char pos = 0;
	char len = 0;	
	if(NULL == dest)
		return ;
	do{
		dest[pos++] = src % 10 + '0';
	}while(src /= 10);
	dest[pos] = '\0';	
	len = strlen((const char*)dest);
	for(pos = 0;pos < len/2;pos++)
	{
		dest[pos] += dest[len-pos-1];
		dest[len-pos-1] = dest[pos]-dest[len-pos-1];
		dest[pos] -= dest[len-pos-1];
	}
}

你可能感兴趣的:(C)