Question

There is a simple encryption scheme that is called “Caesar Cipher”. In a “Caesar Cipher”, the...

There is a simple encryption scheme that is called “Caesar Cipher”. In a “Caesar Cipher”, the letters in a message are replaced by the letters of a “shifted” alphabet. A “Caesar Cipher” encodes a message by shifting each letter in a message by a constant amount of k. If k is 5, A becomes F, B becomes G, C becomes H and so on.
Let's use Queue to encode and decode the encrypt message. Set k as -5 and decipher the following codes by using Linear Queue.
Encrypt message: H T S L W F Y Z Q F Y N T S X
NOTE: Assume the following functions have been implemented in the Queue class. You may USE or CALL any of these functions.
• void Enqueue(char kod);
• void Dequeue();
• char QueueFront();
• char QueueRear();
• bool Empty();
• bool Full();
a. Write the code snippet for main() function to decode the encrypt message. Then, insert the decoded message into the Queue and display the message from the Queue.
The encrypt message is stored as the following:- char code[16] = "HTSLWFYZQFYNTSX";

b. State the result of your output.

I need the code in C++

Homework Answers

Answer #1

#include <iostream>
#include <cstdlib>
#include<cstring>
using namespace std;

// define default capacity of the queue
#define SIZE 20

// Class for queue
class queue
{
   char *arr;     // array to store queue elements
   int capacity;   // maximum capacity of the queue
   int front;     // front points to front element in the queue (if any)
   int rear;     // rear points to last element in the queue
   int count;     // current size of the queue
  
public:
   queue(int size = SIZE);   // constructor
   ~queue();                   // destructor

   void dequeue();
   void enqueue(char x);
   bool Empty();
   bool Full();
   char QueueFront();
};

// Constructor to initialize queue
queue::queue(int size)
{
   arr = new char[size];
   capacity = size;
   front = 0;
   rear = -1;
   count = 0;
}

// Destructor to free memory allocated to the queue
queue::~queue()
{
   delete[] arr;
}

// Utility function to remove front element from the queue
void queue::dequeue()
{
   // check for queue underflow
   if (Empty())
   {
       cout << "UnderFlow\nProgram Terminated\n";
       exit(EXIT_FAILURE);
   }

  
   front = (front + 1) % capacity;
   count--;
}

// Utility function to add an item to the queue
void queue::enqueue(char item)
{
   // check for queue overflow
   if (Full())
   {
       cout << "OverFlow\nProgram Terminated\n";
       exit(EXIT_FAILURE);
   }


   rear = (rear + 1) % capacity;
   arr[rear] = item;
   count++;
}


// Utility function to check if the queue is empty or not
bool queue::Empty()
{
   return (count == 0);
}

char queue::QueueFront()
{
   if (Empty())
   {
       cout << "UnderFlow\nProgram Terminated\n";
       exit(EXIT_FAILURE);
   }
   return arr[front];
}

// Utility function to check if the queue is full or not
bool queue::Full()
{
   return (count == capacity);
}

//Required Part

int main()
{
   // create a queue of capacity 5
   char code[16] = "HTSLWFYZQFYNTSX";
   queue q;
   for(int i=0;i<strlen(code);i++)
   {
       q.enqueue(code[i]);
   }
   queue q1;
   for(int i=0;i<strlen(code);i++)
   {
       char c=q.QueueFront()-5;
       q.dequeue();
       if(c>'Z')
       {
           int a = c-'Z';
           c = 64+a;
       }
       if(c<'A')
       {
           int a = 'A'-c;
           c = 90+a;
       }
       q1.enqueue(c);
   }
   while(!q1.Empty())
   {
       cout<<q1.QueueFront();
       q1.dequeue();
   }
  
   return 0;
}

//Output

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions