Column

mail@pastecode.io avatar
unknown
c_cpp
7 months ago
1.3 kB
1
Indexable
Never
#include<bits/stdc++.h>
using namespace std;

int main(){
    vector<vector<char>> matrix(10, vector<char>(10, 'x'));

    int count=0, lastRow=0;
    string str, cipher, key;
    cout << "Input the normal text: ";
    getline(cin, str);
    cout << "Input the key: ";
    getline(cin, key);
    cout << endl << "Before encryption: " << str << endl;

    for(int i=0; i<10; i++){
        for(int j=0; j<key.length() && count<str.length(); j++){
            if(str[count] != ' '){
                matrix[i][j] = str[count];
                count++;
                lastRow=i;
            }
            else{
                j--;
                count++;
            }
        }
    }

    for(int i=0; i<=lastRow; i++){
        for(int j=0; j<key.length(); j++){
            cout<<matrix[i][j]<<" ";
        }
        cout << endl;
    }

    for(int i=0; i<=lastRow; i++){
        for(int j=0; j<key.length(); j++){
            cout<<matrix[i][j]<<" ";
        }
        cout << endl;
    }
    cout<<endl;

    for (char c:key){
        int col=c-'0'-1; 
        if (col>=0 && col<key.length()){
            for (int i=0; i<=lastRow; i++){
                cipher += matrix[i][col];
            }
        }
    }
    // int num = stoi(str);
    cout<<"After encryption: "<<cipher;

}
Leave a Comment