void interToString(char *str1, int n)
{
char *str = str1;
int i = 0, sign;
if((sign = n) < 0)
sign = -sign;
do
{
str[i++] = sign%10 + '0';
} while ((sign = sign/10) > 0);
if(n < 0)
str[i++] = '-';
str [i++] = 0;
int j = strlen(str) - 1;
int len = j;
i = j/2;
for(;j>i;j--)
{
str[j] ^= str[len - j];
str[len - j] ^= str[j];
str[j] ^= str[len - j];
}
}
#include
//函数名: itoa
//功 能: 把一整数转换为字符串
//用 法: char *itoa(int value, char *string, int radix);
int main(void)
{
int number = 12345678;
char string[25];
itoa(number, string, 10);
printf("integer = %d string = %s\n", number, string);
return 0;
}
方法2:
#include
#include
void reverse(char s[])
{ //字符串反转
int c, i=0, j;
for(j=strlen(s)-1;i
c=s[i];
s[i]=s[j];
s[j]=c;
i++;
}
}
void IntegerToString(char s[],int n)
{
int i = 0,sign;
if((sign=n)<0)//如果是负数,先转成正数
n=-n;
do //从个位开始变成字符,直到最高位,最后应该反转
{
s[i++]=n%10+'0';
}while((n=n/10)>0);
//如果是负数,补上负号
if(sign<0)
s[i++]='-';
s[i]='\0';//字符串结束
reverse(s);
}
int _tmain(int argc, _TCHAR* argv[])
{
int m;
char c[100];
printf("请输入整数m: ");
scanf("%d",&m);
IntegerToString(c,m);
printf("integer = %d string = %s\n", m, c);
return 0;
}
方法3(C++)
#include
#include
using namespace std;
int main()
{
int n=0;
string str="12345";
cout<
cout<
return 0;
}