#include <cstdlib>
#include <iostream>
#include <stdio.h>
using namespace std;
char *inttostring(int num,char *str)//整数转成字符串
{
int i=0;
char temp[10];
while(num!=0)
{
str[i]=num%10+'0';
num=num/10;
i++;
}
str[i]=0;
i--;
int j=0;
while(i>=0)
{
temp[j]=str[i];
j++;
i--;
}
temp[j]=0;
i=0;
while(temp[i]!='\0')//复制回str
{
str[i]=temp[i];
i++;
}
str[i]=temp[i];
return str;
}
int stringtoint(const char *str)//字符串转成整数
{
int num=0;
int i=0;
while(str[i]!='\0')
{
num=num*10+(str[i]-'0');
i++;
}
return num;
}
int main(int argc, char *argv[])
{
int num=12345;
char res[10];
inttostring(num,res);
cout<<"转换后的字符串为"<<res<<endl;
char *sstr="67890";
int resint;
resint=stringtoint(sstr);
cout<<"转换后的整数为:"<<resint<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
(1411)实现字符串与整数的相互转换(不使用itoa和atoi)
最新推荐文章于 2023-04-23 18:21:20 发布