Stack line encryption

I got a job for my C ++ class last week. I think some of you will find this interesting! I managed to get most of the code, but I got stuck and can't figure it out for me to live ... The following are the guidelines for the encryption process that I have to enter into the code:

The sender of the message enters the four-letter word, UDP and another alphabetic word, XXXX .

Then the sender of the message enters the message for encryption.

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

When the scanned character is one of the characters in the UDP , print char and continue to print and embroider the characters at the top of the stack until the stack is empty or the char at the top of the stack is one of the characters in XXXX . When the end of the message occurs, print a character at the top of the stack and continue pop and print from the top of the stack until the stack is empty.

Here's a tip: " GOOD " " LUCK ", this is " SOUNDS SIMPLY TO ME ", or in the form your program will say: " OSDNOT EEM LPMIS SU "

So this is the actual destination.

I am having problems with the last bit:

When the end of the message occurs, print a character at the top of the stack and continue 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 compiling, the final output gives me " OSDNOT ", which is given in the example provided by my professor, so I know that I'm heading to the right track .. Any help would be great, I don’t even know where to start learning the code.

+6
source share
1 answer

Here is the corrected code. You encoded the algorithm incorrectly. I commented on the changes I made to the code. First of all, you didn’t pop out the stack elements when you came across a symbol that was present in the Opera during scanning. Also, at the end of the scan, you did not clear the stack. Include cstring instead of string . As stated 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: it is better to use std :: stack and std :: string.

+3
source

Source: https://habr.com/ru/post/917927/


All Articles