Question: Write a program that will encode and decode an input string using a key string. As each letter of the input string is encoded or
Write a program that will encode and decode an input string using a key string. As each letter of the input string is encoded or decoded, the next character in the key string is to be used. When the end of the key string is found, the program needs to start back at the beginning of the key string as needed.
Starter code from the teacher:
#include
using namespace std;
//xor operator ^: Hash code applied twice regenerates the original value: m is message character and h is hash // x=m^h and y=x^h then y (decoded message) is the same as m
void coder(char *x, char key,int length); char *getMessage(char *request);
int main() { char *m = getMessage("Enter message to encode: "); int length = strlen(m); cout << length << endl; //char *k = getMessage("Enter key ");//read in a key of arbitrary length coder(m, 'A', length);//A is the key. Encode message with key string cout << m << endl; coder(m, 'A', length);//Decode message. Decode message with key string cout << m << endl; delete(m); //no leaks // delete(k); return 0; }
void coder(char *message, char key, int length)//make key an array { for (int i = 0; i < length; i++) message[i] = message[i]^key;//cycle through the key }
char *getMessage(char *request) { char temp[100], *p; cout << request << " "; cin >> temp; p = new char(strlen(temp)+1);//+1 for terminating null strcpy(p, temp); return p; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
