首页 > 其他 > 详细

数字转换为对应的字符串,itoa

时间:2015-11-24 06:21:06      阅读:181      评论:0      收藏:0      [点我收藏+]

实现一个函数itoa(int n,char s[]),将整数n这个数字转换为对应的字符串,保存到s中


#include<stdio.h>
#include<stdlib.h>

void my_itoa(char arr[],int n)//数字转换为对应的字符串
{
	 int i = 0;
	 int start = 0;
	 int end = 0;
	
	 while (n)
	 {
		 arr[i] = (n % 10)+‘0‘;
		 n = n / 10;
		 i++;
	 }
	 end = i - 1;
	 
	 while (start < end)
	 {
		 char tmp = arr[start];
		 arr[start] = arr[end];
		 arr[end] = tmp;
		 start++;
		 end--;
	 }
	 arr[i] = ‘\0‘;
}

int main()
{
	int a = 1234;
	char s[10] = {0};
	
	my_itoa(s,a);
	
	printf("%s\n", s);
	
	system("pause");
	return 0;
}


本文出自 “无以伦比的暖阳” 博客,请务必保留此出处http://10797127.blog.51cto.com/10787127/1716180

数字转换为对应的字符串,itoa

原文:http://10797127.blog.51cto.com/10787127/1716180

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!