c++ - 使用额外列的行转置密码

标签 c++

大家好我正在尝试制作一个程序,该程序接受一些用户输入并将其映射到二维数组,然后通过混合列对其进行加密。例如,如果用户输入“我的名字是弗雷德”,程序会创建一个 3x6 的数组,用 y 填充最后一列,用 x 填充剩余的空格,所以它应该是这样的

我的名字 炸薯条 edxxxx

相反,我结束了

我南 eisfr edxx

#include <iostream>
#include<cctype>
#include<algorithm>
using namespace std;

main(){
string input;

cout << "Enter information to be encrypted" << endl;
getline(cin,input);
input.erase(std::remove (input.begin(), input.end(), ' '), input.end());

int columns = 6;
int rows;
  if (input.size() <= 5){
    rows = 1;
  }
  else if (input.size()% 5 > 0){
    rows = input.size()/5 + 1;
  }
  else
    rows = input.size()/5;

char message[rows][columns];
int place = 0;

    for(int i = 0; i < rows; i++){
      for(int j = 0; j < (columns-1); j++){
        if(place <= input.size()){
        message[rows][columns] = input[place];
        }
        else {
        message[rows][columns] = 'x';
        }
        place++;
        message[rows][5] = 'y';
        cout << message[rows][columns];
      }
        cout << endl;
    }


}

最佳答案

这应该可以做到..

#include <iostream>
#include <cctype>
#include <algorithm>
using namespace std;

int main()
{
    string input;

    cout << "Enter information to be encrypted" << endl;
    getline(cin,input);
    input.erase(std::remove (input.begin(), input.end(), ' '), input.end());

    int columns = 6;
    int rows;
    if (input.size() <= 5){
        rows = 1;
    }
    else if (input.size()% 5 > 0){
        rows = input.size()/5 + 1;
    }
    else
        rows = input.size()/5;

    char message[rows][columns];

    for(int i = 0; i < rows; i++){
        for(int j = 0; j < (columns-1); j++){
            if ((i*5 + j) < int(input.size())){
                message[i][j] = input[i*5 + j];
            }
            else {
                message[i][j] = 'x';
            }
            // place++;
            if (i != rows-1) message[i][5] = 'y';
            else message[i][5] = 'x';
            // cout << "i: " << i << " | j: " << j << " | " << message[i][j] << endl;
        }
        cout << endl;
    }
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            cout << message[i][j];
        }
        cout << " ";
    }
    cout << endl;
}

关于c++ - 使用额外列的行转置密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26130843/

相关文章:

c++ - 以下两种返回 C++ 对象的方法是否相同?

c++ - Gnome 的 gslist 比 std::forward_list 有什么优势?

C++ LZMA SDK : Uncompress function for LZMA2 compressed file

c++ - 释放指向结构的指针的内存

c++ - 指针还是索引?

c++ - 从 `if constexpr` 分支扩展对象生命周期/范围

c++ - 用于反转 C++ 中以零结尾的数字

c++ - GCC 9.3是否支持C++ 20 std::format?

c++ - 使用 Qt 播放实时视频流

c++ - C++中类型和类之间的区别?