Need some help with this C++ coding. Assignment: Here you will begin to develop
ID: 3665685 • Letter: N
Question
Need some help with this C++ coding.
Assignment:
Here you will begin to develop a crytographic scheme that will encode a message. In order to do that, you will convert a single character into its ascii code, add a certain integer value to it, and convert back into a character. The mapping will be to add CRYPT_KEY. For example, if CRYPT_KEY =2, the mapping would take a---->c, and also take z---->B. Note that ascii values greater that z get "wrapped around" starting again at A. Your program must:
-Define a constant CRYPT_KEY to any value integer value you want
-Use modulus operator (%) to maintain a mapping between ascii A and z.
-Use static_cast to convert between data type char and int.
In the example shown, I have CRYPT_KEY defined 10.
Enter Letter: x
encrypted letter:h
Explanation / Answer
Logic: you need to check if the ascii value of CRYPT_KEY + given letter is greator than ascii value of 'z' or not. If it is smaller than 'z' then you need to just add the key to the given letter , otherwise you need to use modulus operator to get the correct value. There is no need to type cast character to integer because character is actually an integer. I mean characters are stored as a integer in the memory. Hence, when you are storing character 'a' then its ascii value (which is 97) is stored in the memory.
Here is my code:
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
const int CRYPT_KEY=2;
char letter;
char encrypted_letter;
cout<<"Enter any letter"<<endl;
cin>>letter;
if((letter+CRYPT_KEY)<='z')
encrypted_letter=letter+CRYPT_KEY;
else
encrypted_letter=((letter+CRYPT_KEY)%'z')+'A'-1;
cout<<encrypted_letter<<endl;
return 0;
}
If you have any doubt then comment below and please don't forget to give me feedback
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.