数字转换为字符串

9.1华为笔试上机考试啊,赶紧复习下字符串。

网上参考了下,自己写了个实现:

#include<iostream>
#include "stdio.h"
using namespace std;
char temp[100];
void my_swap(char &a,char &b)
{
    char temp1 = a;
	a = b;
	b = temp1;
}
void res_num(int num)
{
	int i = 0;
	while (num)
	{
       temp[i++] = num%10 + '0';
       num /= 10;
	}
	cout << temp << endl;
	int j = 0;
	--i;
    while (j < i)
    {
		my_swap(temp[i],temp[j]);
		++j;
		--i;
    }
}

int main()
{
	res_num(10011828);
	cout << temp;
}

  

posted @ 2011-08-29 10:46  hailong  阅读(525)  评论(1编辑  收藏  举报