Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encrypting a string with a stack

I received an assignment for my C++ class last week. I think some of you will find it interesting! I managed to get most of the code down but I'm stuck and cannot figure this out for the life of me... Below are the guidelines for the encrypting process I must put into code:

The message sender inputs a four letter word, CCCC, and another four letter word, XXXX.

The message sender then inputs the message to be encrypted.

The program scans the message one char at a time and each char is pushed in a stack until either the scanned character is in the word CCCC or the end of the message is encountered.

When the scanned character is one of the chars in CCCC, print that char and continue to print and pop the chars at the top of the stack until either the stack is empty or the char at the top of the stack is one of the chars in XXXX. When the end of the message is encountered, print the character at the top of the stack and continue to pop and print from the top of the stack until the stack is empty.

Here is a hint: "GOOD" "LUCK", it "SOUNDS SIMPLE TO ME", or as your program would say: "OSDNOT EEM LPMIS SU"

So that is the actual assignment.

What I am having trouble with is the last bit:

When the end of the message is encountered, print the character at the top of the stack and continue to pop and print from the top of the stack until the stack is empty.

now here is the code I have so far:

#include <string>
#include <iostream>
using namespace std;
class Stack
{
   private:
   char Chars[50];
   int top;
   public:
   int push(char);
   char pop();
   bool isEmpty();
   bool isFull();
   Stack()
   {
      top = 0;
   }
};

int main()
{
Stack theStack;
   char word1[4];
   char word2[4];
   for(int i=0; i < 4; i++){
      word1[i] = ' ';
      word2[i] = ' ';
   }
   char message[500];   
   cout << "Please enter a 4 letter word: ";
   cin >> word1;
   while(word1[4] || !word1[3])
   {
      cout << "Word must be 4 chars long. Try again: ";
      cin >> word1;
   }
   cout << "Please enter another 4 letter word: ";
   cin >> word2;
   while(word2[4] || !word2[3])
   {
      cout << "Word must be 4 chars long. Try again: ";
      cin >> word2;
   }
   cout << "Please enter the phrase to be encrypted (50 chars max): ";
   cin.ignore(1000, '\n');
   cin.getline(message,500);
   int length = strlen(message);
   int count = 0;
   char finalMsg[length];
   //scanner
   for(int i = 0; i < length; i++)
   {
      if(message[i] == word1[0] ||
         message[i] == word1[1] ||
         message[i] == word1[2] ||
         message[i] == word1[3])
      {
         finalMsg[count] = message[i];
         count++;
         if(message[i-1] != word2[0] ||
            message[i-1] != word2[1] ||
            message[i-1] != word2[2] ||
            message[i-1] != word2[3])
         {
            finalMsg[count] =  message[i-1];
            count++;
         }
      }
      else
      {
         theStack.push(message[i]);
      }
   }
   cout << finalMsg << endl;
return 0;
}

int Stack::push(char data)
{
   Chars[top] = data;
   top++;
return top;
}

char Stack::pop()
{
   char ret = Chars[top-1];
   top--;
return ret;
}

bool Stack::isEmpty()
{
   if(top <= 0)
      return true;
   else return false;
}

bool Stack::isFull()
{
   if(top >= 50)
      return true;
   else return false;
}

When compiled, the final output gives me "OSDNOT" which is in the example provided by my professor, so I know I'm heading down the right track.. Any help would be great, I don't even know where to begin to examine the code.

like image 864
rcorrie Avatar asked Jun 13 '12 03:06

rcorrie


People also ask

How can I encrypt a string?

Encryption Approach:Find the length L of the string. Find the ceil and floor values of √Length and assign them to the variables. Check if the product of the two variables >= Length, if not then increments the variable having a lesser value by 1. Create a 2D matrix and fill the characters of the string row-wise.


1 Answers

Here is the corrected code. You didn't code the algorithm right. I have commented the changes i have made in the code. first of all, you didn't pop out the elements of the stack when you encountered a character present in CCCC while scanning. Also at the end of scanning, you didn't empty the stack. Include cstring instead of string. As pointed out in the comments, your declaration for word1 and word2 is incorrect.

   char finalMsg[200];
   //scanner
   for(int i = 0; i < length; i++)
   {
      if(message[i] == word1[0] ||
         message[i] == word1[1] ||
         message[i] == word1[2] ||
         message[i] == word1[3])
      {
         finalMsg[count] = message[i];
         count++;

         //pop out elements from the stack till it is empty or an character of XXXX is encountered
         while(!theStack.isEmpty())     
         {
             char tmp=theStack.pop();
             if(tmp==word2[0] ||
                tmp==word2[1] ||
                tmp==word2[2] ||
                tmp==word2[3])
                {
                    theStack.push(tmp);
                    break;
                }
            finalMsg[count++]=tmp;
         }

      }
      else
      {
         theStack.push(message[i]);
      }
   }

  //empty the stack
   while(!theStack.isEmpty())
   {
       finalMsg[count++]=theStack.pop();
   }
   finalMsg[count++]=0;
   cout << finalMsg << endl;

PS: Better use std::stack and std::string.

like image 68
nims Avatar answered Oct 07 '22 12:10

nims