Help with this substitution program- line 3 of the output is different.pdf
Help with this substitution program- line 3 of the output is different.pdf
Help with this substitution program- line 3 of the output is different.pdf
Help with this substitution program- line 3 of the output is different.pdf
Help with this substitution program- line 3 of the output is different.pdf
Nächste SlideShare
Please help reviewing the following code for caesar cipher (ccipher-h.pdfPlease help reviewing the following code for caesar cipher (ccipher-h.pdf
Wird geladen in ... 3
1 von 5

Más contenido relacionado

Más de gaurav444u(20)

Help with this substitution program- line 3 of the output is different.pdf

  1. Help with this substitution program, line 3 of the output is different than it should be and I can't find the reaso why. Here's the code, the input file, and the output file. #include "cipher.h" /* Cheshire smile implementation. It only contains the cipher alphabet */ struct Cipher::CipherCheshire { string cipher_alpha; }; /* This function checks the cipher alphabet to make sure it's valid */ bool is_valid_alpha(string alpha); // ------------------------------------------------------- // Cipher implementation /* Default constructor This will actually not encrypt the input text because it's using the unscrambled alphabet */ Cipher::Cipher() { // TODO: Implement this default constructor smile = new CipherCheshire(); smile->cipher_alpha = "zyxwvutsrqponmlkjihgfedcba"; } /* This constructor initiates the object with a input cipher key */ Cipher::Cipher(string cipher_alpha) { // TODO: Implement this constructor smile - new CipherCheshire(); if (is_valid_alpha(cipher_alpha)) { smile->cipher_alpha = cipher_alpha; } else { cout << "Invalid alpha" << endl; exit(EXIT_FAILURE); } }
  2. /* Destructor */ Cipher::~Cipher() { // TODO: Implement this constructor delete(smile); } /* This member function encrypts the input text using the intialized cipher key */ string Cipher::encrypt(string raw) { cout << "Encrypting..."; string retStr = ""; char ch, tmp; int pos; int i; int size = raw.length(); // TODO: Finish this function for (i = 0; i < size; i++) { tmp = ' '; ch = raw.at(i); if (ch != ' ') { por = find_pos(smile->cipher_alpha, ch) if (ch >= 65 && ch <= 90) { tmp = 65 + pos; } else { tmp = 97 + pos; } } retStr += tmp; } cout << "Done" << endl; return retStr; } /* This member function decrypts the input text using the intialized cipher key */ string Cipher::decrypt(string enc) { string retStr = "";
  3. char ch, tmp; int pos; int i; int size = enc.length(); cout << "Decrypting..."; // TODO: Finish this function for (i = 0; i < size; i++) { tmp = ' '; ch = enc.at(i); if (ch != ' ') { pos = find_pos(smile->cipher_alpha, ch); if (ch >= 65 && ch <= 90) { tmp = 65 + pos; } else { tmp = 97 + pos; } } retStr += tmp; } cout << "Done" << endl; return retStr; } // ------------------------------------------------------- // Helper functions /* Find the character c's position in the cipher alphabet/key */ unsigned int find_pos(string alpha, char c) { unsigned int pos = 0; int i; int size = alpha.length(); // TODO: You will likely need this function. Finish it. char ch = tolower(c); for (i = 0; i < size; i++) { if (ch == alpha.at(i)) { por = i; break; } }
  4. return pos; } /* Make sure the cipher alphabet is valid - a) it must contain every letter in the alphabet b) it must contain only one of each letter c) it must be all lower case letters ALL of the above conditions must be met for the text to be a valid cipher alphabet. */ bool is_valid_alpha(string alpha) { bool is_valid = true; if(alpha.size() != ALPHABET_SIZE) { is_valid = false; } else { unsigned int letter_exists[ALPHABET_SIZE]; for(unsigned int i = 0; i < ALPHABET_SIZE; i++) { letter_exists[i] = 0; } for(unsigned int i = 0; i < alpha.size(); i++) { char c = alpha[i]; if(!((c >= 'a') && (c <= 'z'))) { is_valid = false; } else { letter_exists[(c - 'a')]++; } } for(unsigned int i = 0; i < ALPHABET_SIZE; i++) { if(letter_exists[i] != 1) { is_valid = false; } } } return is_valid; } INPUT: EXPECTED OUTPUT: CURRENT OUTPUT: zyxwvutsrqponmlkj ihgfedcba Sometimes it is the people no one can imagine anything of who do the things no one can imagine Hlnvgrnvh rg rh gsv kvlkov ml lmv Xzm rnztrmv zmbgsrmt lu dsl wl gsv gsrmth ml lmv Xzm rnztrmv Sometimes it is the people no one can imagine anything
  5. of who do the things no one can imagine Sometimes it is the people no one can imagine anything of who do the things no one can imagine Sometimes it is the people no one can imagine anything of who do the things no one can imagine Hlnvgrnvh rg rh gsv kvlkov ml lmv xzm rnztrmv zmbgsrmt lu dsl wl gsv gsrmth ml lmv xzm rnztrmv Sometimes it is the people no one can imagine anything of who do the things no one can imagine Hlnvgrnvh rg rh gsv kvlkov ml lmv Xzm rnztrmv zmbgsrmt lu dsl wl gsv gsrmth ml lmv Xzm rnztrmv Sometimes it is the people no one can imagine anything of who do the things no one can imagine