CS 10 ? Assignment 3: Cipher Single Collaboration Policy We encourage collaborat
ID: 654287 • Letter: C
Question
CS 10 ? Assignment 3: Cipher Single
Collaboration Policy
We encourage collaboration on various activities such as lab, codelab, and textbook exercises.
However, no collaboration between students is allowed on the programming assignments. Please be sure to read and understand our full policy at: Full Collaboration Policy
Submission Instructions
Submit to RSub testing, feedback and grading.
Assignment Specifications
Decryption and encryption is a very useful activity to hide messages or data. Encryption is utilized to
store passwords, store user information and in many other means when the sender does not want
the message to be easily readable to anyone other than the intended reader.
Your Assignment
You must write a program to encrypt the first character of a word. The word will be entered by the
user. Additionally, the user will enter a string value to be utilized as a translation map when
determining how to translate the character.
Alphabetic Positions
Positions start at zero. If the letter is 'a' then the position of the letter within the alphabet is 0
and if the letter is 'c' the position is 2 and so on through 'z' at position 25
Algorithm
? Acquire the translation map
? If keyword default is entered, utilize the default map.
? Validate the size of the map (26 characters)
? Acquire the word to encrypt, validate encryption is possible.
? Validate whether the first character in the word can be encrypted
? Perform encryption
? Encryption: Convert the first character in the word to a character in the map
Store the default map into your map variable: "zyxwvutsrqponmlkjihgfedcba"
The first character of the word must be a lowercase letter
Get the character from the word
Calculate the character's alphabetic position
Replace the character in the word with the character in the map at the calculated
position.
Hints/Tips
? Implement the algorithm in the specified order.
? If you get to an error point, immediately exit the program (within main just return0;)
? Use only a single map variable, assigning a proper value based the value of user input.
Encryption Example
If your translation map is "9876543210abcdefghijklmnop" and your word is "about", your task is
to convert the first character. Thus, you need to convert the 'a' to '9' resulting in "9bout". The
conversion comes from 'a' being the first letter (position 0) in the alphabet, so the character gets
converted to the first character (position 0) in the map, '9'. Output Requirements
? Valid output will always start with a keyword/phrase. After the keyword or phrase should be
the result. An example of this keyword: result combination is shown in the example runs.
? Error:
invalid translation map size.
encryption cannot be performed.
? Encrypted word:
Question
What combination of inputs will result in the second error? ("encryption cannot be performed.")
Example Runs (User input has been bolded and underlined to help differentiate typed input from program output.)
$g++encrypt.cpp
$./a.out
Whatisthetranslationmap(type'default'tousedefault):fghjkl
Error:invalidtranslationmapsize.
$
$./a.out
Whatisthetranslationmap(type'default'tousedefault):default
Whatisthesinglewordtotranslate:hello
Encryptedword:sello
$
$./a.out
Whatisthetranslationmap(type'default'tousedefault):poiuytrewqasdfghjklzxcvbnm
What is the singlewordtotranslate:hello
Encryptedword:eello
$
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
int getPosition(char ch);
int main()
{
string alphabet12 = "abcdefghijklmnopqrstuvwxyz";
string defaultMap = "zyxwvutsrqponmlkjihgfedcba";
string method;
string translationMap;
string word;
int pos;
// Acquire the method to perform (encryption or decryption).
cout << "What is the method (encryption or decryption)? ";
cin >> method;
if(method != "encryption" && method != "decryption")
{
cout << "Error: Invalid method choice." << endl;
system("pause");
return 0;
}
cout << "What is the translation map (type 'default' to use default): ";
cin >> translationMap;
if(translationMap == "default")
translationMap = defaultMap;
if(translationMap.size() != 26)
{
cout << "Error: Invalid translation map size." << endl;
system("pause");
return 0;
}
// Acquire the word to encrypt or decrypt,
cout << "What is the single word to translate: ";
cin >> word;
for(int i = 0; i < word.size(); i++)
{
if(getPosition(word.at(i)) == -1)
{
if(method == "encryption")
cout << "Error: Encryption cannot be performed." << endl;
else // if(method == "decryption")
cout << "Error: Decryption cannot be performed." << endl;
system("pause");
return 0;
}
}
if(method == "encryption")
{
for(int i = 0; i < word.size(); i++)
{
int pos = getPosition(word.at(i));
word[i] = translationMap[pos];
}
cout << "Encrypted word: " << word << endl;
}
else
{
for(int i = 0; i < word.size(); i++)
{
int pos = translationMap.find(word.at(i));
word[i] = (char)(97 + pos);
}
cout << "Decryption word: " << word << endl;
}
cout << endl;
system("pause");
return 0;
}
int getPosition(char ch)
{
if(ch >= 'a' && ch <= 'z')
return (ch - 'a');
else
return -1;
}