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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
/*
* ceasar
*
* Get the key
* Get the plaintext
* Encipher the text
One character
Entire plaintext
isapha
isupper
islower
* Print cipher text message
* Start: ASCII values
* Encipher: Alphabetical index
* Print: ASCII values
*
* c[i] = (p[i] + k) % 26
*
*/
#include <cstdio> // printf_s scanf_s
#include <cstdlib> // atoi
#include <cctype> // isupper, islower
#include <iostream> // cin, cout
#include <string> // getline
using namespace std;
int main(int argc, char* argv[])
{
// error check
if (argc != 2)
{
printf_s("Illegal Usage: ./ascimath key");
return 1;
}
// key is the second command line argument and is converted from char to integer
int key = atoi(argv[1]);
// variable
string cstr ("This is C++!");
string encrpt;
int incr;
int len = cstr.length();
// Get message from user
// printf_s("Enter the message: ");
// printf_s("%i\n", cstr);
// getline(cin, cstr);
for (incr = 0; incr < len; incr++)
{
cout << "working character: " << cstr[incr] << endl;
// encrypt message
if (isalpha(cstr[incr]))
{
// Work with lower case
if (islower(cstr[incr]))
{
printf_s("incr is %i\n", incr);
int x = ((cstr[incr] - 'a') + key) % 26;
printf_s("x in islower is: %i\n", x);
encrpt[incr] = x + 'a';
}
else
{
printf_s("incr is %i\n", incr);
// change ASCII value to encrypt value
int x = ((cstr[incr] - 'A') + key) % 26;
printf_s("x in isupper is: %i\n", x);
encrpt[incr] = x + 'A';
}
}
else
{
encrpt[incr] = cstr[incr];
printf_s("incr is %i\n", incr);
}
printf_s("incr back to while is %i\n", incr);
}
// Print encrytion.
printf_s("Encrypted string is: ");
for (int i = 0; i < len; i++)
{
printf_s("%c", encrpt[i]);
}
printf_s("\n");
// Print decrytion.
printf_s("Decrypted string is: ");
for (int i = 0; i < len; i++)
{
printf_s("%c", cstr[i]);
}
printf_s("\n");
}
|