需要我当前的字符输出,这是它在C++中的整数值
Need my current output in Characters which are its int values in C++
d[i] = char(c[i]);
在下面的示例中,这对我不起作用。
我需要将我的输出转换为其字符值,但是在使用 char(int)
之后,它仍然仅使用 int
数据类型给出输出。
#include <bits/stdc++.h>
using namespace std;
int main()
{
string str;
cin>>str;
int size=str.size();
int len=0;
if (size % 2 == 0)
{
len=size/2;
}
else
{
len=(size/2)+1;
}
int a[len],b[len],c[len],d[len],x,y;
int i=0,j=size-1;
while(i<len)
{
x=(int)str[i];
y=(int)str[j];
if (i == j)
{
a[i]=x;
}
else
{
a[i]=x+y;
}
b[i]=a[i]%26;
c[i]=x + b[i];
d[i]=char(c[i]);
cout<<"l : "<<d[i]<<endl;
i++;
j--;
}
return 0;
}
代码失败,因为您将值存储在 int[]
数组中。 d[i]=char(c[i]);
是没有用的,因为您所做的只是将int
转换为char
,然后再转换为int
。然后,将数组值按原样输出为int
值,而不是将它们转换回实际的char
值。
尝试更多类似的东西:
#include <vector>
#include <string>
using namespace std;
int main()
{
string str;
cin >> str;
int size = str.size();
int len = (size / 2) + (size % 2);
// VLAs are a non-standard compiler extension are are not portable!
// Use new[] or std::vector for portable dynamic arrays...
//
// int a[len], b[len], c[len];
// char d[len];
//
std::vector<int> a(len), b(len), c(len);
std::vector<char> d(len);
int x, y, i = 0, j = (size-1);
while (i < len)
{
x = (int) str[i];
y = (int) str[j];
if (i == j)
{
a[i] = x;
}
else
{
a[i] = x + y;
}
b[i] = a[i] % 26;
c[i] = x + b[i];
d[i] = (char) c[i];
cout << "l : " << d[i] << endl;
++i;
--j;
}
return 0;
}
相关文章:
- 如何反转整数参数包
- enum是C++中的宏变量还是整数变量
- 努力将整数转换为链表。不知道我在这里做错了什么
- 整数不会重复超过随机数
- 在C++中手动调整数组大小
- 检查输入是否不是整数或数字
- C++使用整数的压缩数组初始化对象
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 通过套接字[TCP]传输数据 如何在C / C ++中打包多个整数并使用send() recv()传输数据
- 如何只允许用户输入正整数
- 如何在c++中从文本文件中逐行读取整数
- C++:如何循环通过向量中的整数元素
- 我可以信任表示整数的浮点或双精度来保持精度吗
- 序列化,没有库的整数,得到奇怪的结果
- 在一定长度后从数组中打印时缺少整数
- std::当在256字节边界上写入整数时,流的奇怪行为
- 内联程序集printf将整数解释为地址
- 是否基于数组B整数打印数组A中的整数
- 如何在C++中将整数转换为其数字数组