-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesar_encode.cpp
More file actions
82 lines (68 loc) · 1.81 KB
/
caesar_encode.cpp
File metadata and controls
82 lines (68 loc) · 1.81 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <string>
#include <fstream>
const char lowerrange = 'z' - 'a';
const char upperrange = 'Z' - 'A';
char encode_char( const char & c, int rotation )
{
char result = c;
if( c >= 'a' && c <= 'z' )
{
result -= 'a';
result += rotation;
result %= lowerrange;
result += 'a';
}
else if ( c >= 'A' && c <= 'Z' )
{
result -= 'A';
result += rotation;
result %= upperrange;
result += 'A';
}
return result;
}
std::string encode( std::string cleartext, int rotation )
{
std::string ciphertext = "";
for( auto c = cleartext.begin(); c != cleartext.end(); ++c )
{
ciphertext += encode_char( *c, rotation );
}
return ciphertext;
}
int main( int argc, char *argv[] )
{
if( argc < 4 )
{
std::cerr << "not enough arguments; usage; caeser_encode [path_to_cleartext] [rotation] [path_to_output_file]" << std::endl;
return 1;
}
std::fstream cleartext_file( argv[1] );
if( !cleartext_file.is_open() )
{
std::cerr << "couldn't open file for reading: " << argv[1] << std::endl;
return 1;
}
std::fstream ciphertext_file;
ciphertext_file.open( argv[3], std::fstream::out );
if( !ciphertext_file.is_open() )
{
std::cerr << "couldn't open file for writing: " << argv[3] << std::endl;
return 1;
}
int rotation = std::stoi( argv[2] );
if( rotation < 1 || rotation > 25 )
{
std::cerr << "provide a rotation from 1 to 25" << std::endl;
return 1;
}
std::string ciphertext = "";
std::string line = "";
while( getline( cleartext_file, line ) )
{
ciphertext_file << encode( line, rotation ) << std::endl;
}
std::cout << ciphertext << std::endl;
return 0;
}