1.用编程的方法将整数转化成字符串
整数转化成字符串,可以采用加‘0’,再逆序的办法,整数加'0'就会隐性转换成char类型的数。
#include <iostream>
#include <stdio.h>
using namespace std;
int main(void)
{
int num = 12345;
char temp[5];
char str[5];
int i = 0;
int j = 0;
while(num)
{
temp[i] = num%10 + '0';
i++;
num = num/10;
}
temp[i] = '/0';
i = i - 1;
while(i >= 0)
{
str[j] = temp[i];
i--;
j++;
}
str[j] = '/0';
cout<<str<<endl;
cin>>num;
}
2.用编程的方法将字符串转换成整数类型
字符串转化成整数,可以采用减‘0’再乘10累加的办法,字符串减‘0’就会隐性转化成int类型的数。
#include <iostream>
#include <stdio.h>
using namespace std;
int main(void)
{
char str[] = "12345";
int num = 0;
int i = 0;
while(str[i])
{
num = num*10 + (str[i] - '0');
i++;
}
cout<<num<<endl;
cin>>num;
}