Ceasar Cypher不会计算空间

Ceasar Cypher would not count spaces

本文关键字:计算 空间 Cypher Ceasar      更新时间:2023-10-16

我正在编写一个具有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是

到目前为止,我的结果:输入" E"或" D"以编码或解码。退出的其他钥匙:E什么是:taf vf结果:GNS

当您遇到空间时,您有效地增加了index - 一次在该if子句中,然后在循环的末端再次递增。这具有跳过(未编码(的净效果。

只需删除整个if(in[index] == ' ')子句即可。其余代码已经使任何不属于四个特殊检查的范围的角色保持不变 - 包括空间。