gpt4 book ai didi

c++ - Caesar Cypher 不会计算空格

转载 作者:行者123 更新时间:2023-11-27 23:39:04 25 4
gpt4 key购买 nike

我正在编写一个具有 4 个不同函数的程序,其中一个询问用户是否要编码/解码,另一个询问在动态内存中输入字符串、实际解密函数和输出函数。我做了一切,但我遇到的唯一问题是考虑输入的单词中是否有空格。

在我的解密函数中,我写道,如果给定单词​​的索引有一个空格,那么它将继续并跳过该索引。编辑:我现在已经包含了输入函数和读取单词并对其进行加密的输出函数。

string *input(){
string *temp = new string;
cout<<"What is the word: ";
getline(cin, *temp);
cin >> *temp;
return temp;
}
string output(string *in){
string cypher;
cypher = decryption(*in);
cout<<"Result: "<<cypher<<endl;
return cypher;
}

string decryption(string in){
int inputSize = in.size();
int index = 0;
while(index != inputSize){
if(in[index] == ' '){//possibly something wrong with this if statement
index++;
}else if(in[index] >= 97 && in[index] <= 109){
in[index]= in[index]+13;
}else if(in[index] >=110 && in[index] <=122){
in[index] = in[index]-13;
}else if(in[index] >=65 && in[index] <=77){
in[index] = in[index]+13;
}else if(in[index] >=78 && in[index] <=90){
in[index] = in[index]-13;
}
index++;
}
return in;
}

预期结果:输入“e”或“d”进行编码或解码。其他退出键:e什么是字:字母表结果:nycunorg

输入“e”或“d”进行编码或解码。其他退出键:e什么词:TAF VF结果:GNS IS

到目前为止我的结果:输入“e”或“d”进行编码或解码。其他退出键:e什么词:TAF VF结果:GNS

最佳答案

当您遇到空格时,您实际上将 index 递增了两次 - 一次是在 if 子句中,另一次是在循环结束时。这具有跳过(未编码)空格后面的字符的实际效果。

简单地删除整个 if(in[index] == ' ') 子句。其余代码已将不属于四个特别检查范围的任何字符保持不变 - 包括空格。

关于c++ - Caesar Cypher 不会计算空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56918836/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com