整數轉化爲字符串

#include "stdafx.h"

void int2str(int num, char *s);

int _tmain(int argc, _TCHAR* argv[])
{
    int a = 1234 ;
    char *s = (char *) malloc(sizeof(char)*10);

    int2str(a, s);

    //cout << s <    return 0;
}

void int2str(int num, char *s)
{
    int len = 0;
    char temp;
    while (num)
    {
        *(s++) = num%10 + '0';
        num = num/10;
        len ++;
    }
    *(s) = '/0';
    s = s- len ;

    for (int i = 0; i < len/2; i++)
    {
        temp = *(s + i);
        *(s + i) = *(s + len - 1 -i);
        *(s + len - 1 -i) = temp;
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章