整数和字符串之间转换


#include <iostream>

using namespace std;

思想是一个整数加上'0'自动变成字符型,注意然后要输出

void main()
{
 int num = 12345;
 int i = 0,j = 0;
    char temp[6],str[6],string[6];

 itoa(num,string,10);
 //string[5] = 0;
 printf("num = %d/n string = %s/n",num,string);
 
    while(num)
 {
  temp[i] = num%10+'0';
  i++;
  num = num/10;
 }
 temp[i] = 0,   //字符串结尾加0
 printf("temp = %s/n",temp); //遇到字符串结尾0结束输出
 i = i-1;
 while(i>=0)
 {
  str[j++] = temp[i--];
 }
 str[j] = 0;
 printf("str = %s/n",str);
 
}

 


#include <iostream>

using namespace std;

void main()
{
 int num = 0,i=0;
 char str[6] = {"12345"};
    printf("str = %s/n",str);
 printf("str[5] = %d/n",str[5]);
 printf("length = %d/n",strlen(str));
 while(str[i])
 {
  num = num*10 + str[i]-'0';
  i++;
 }
 printf("num = %d/n",num);
}

//字符串有多种赋值方式
// str[6] = {'1','2','3','4','5'};
// str[6] = {"12345"}
// str[6] = "12345"    //字符串数组会自动在尾部加个/0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值