整数字符串转化

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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值