-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMessage Encryption.cpp
More file actions
58 lines (49 loc) · 1.05 KB
/
Message Encryption.cpp
File metadata and controls
58 lines (49 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
string s;
int c;
getline(cin, s);
cin >> c;
int r = s.length() / c; // Number of rows
vector<vector<char>> m(r, vector<char>(c));
int idx = 0;
// Fill the matrix row by row
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
m[i][j] = s[idx++];
}
}
// Reverse the characters in even rows (1-based index)
for (int i = 1; i < r; i += 2) {
for (int j = 0, k = c - 1; j < k; j++, k--) {
char t = m[i][j];
m[i][j] = m[i][k];
m[i][k] = t;
}
}
// Collect the result by traversing column-wise
string res = "";
for (int j = 0; j < c; j++) {
for (int i = 0; i < r; i++) {
res += m[i][j];
}
}
// Print the final result
cout << res << endl;
return 0;
}
Example 1:
Input:
midinadiazne
3
Output:
madeinindiaz
Example 2:
Input:
loaesfbnaiordilertenrdhdw
5
Output:
lionroaredandthebirdsflew