Update columnar-transposition input validation
+ Add keyWord and message validation in columnar-transposition example + Add quotes around output to help show leading / trailing spaces
This commit is contained in:
		
							parent
							
								
									c8683680dd
								
							
						
					
					
						commit
						909bf3278e
					
				@ -23,6 +23,7 @@
 | 
				
			|||||||
// Main program loop
 | 
					// Main program loop
 | 
				
			||||||
int main (const int argc, const char * argv[]) {
 | 
					int main (const int argc, const char * argv[]) {
 | 
				
			||||||
  // Left some test cases I used commented out :)
 | 
					  // Left some test cases I used commented out :)
 | 
				
			||||||
 | 
					  // + Keywords and messages can be URLs, include spaces, symbols, numbers, etc
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // Using example keyword from www.braingle.com
 | 
					  // Using example keyword from www.braingle.com
 | 
				
			||||||
  // + The embedded example there doesn't seem to support numbers in keywords :(
 | 
					  // + The embedded example there doesn't seem to support numbers in keywords :(
 | 
				
			||||||
@ -47,7 +48,7 @@ int main (const int argc, const char * argv[]) {
 | 
				
			|||||||
        // Take input for encrypting a message
 | 
					        // Take input for encrypting a message
 | 
				
			||||||
//        result = cData.Encrypt();
 | 
					//        result = cData.Encrypt();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        std::cout << "Encrypted message: " << result << std::endl;
 | 
					        std::cout << "Encrypted message: \"" << result << "\"\n";
 | 
				
			||||||
        exit = true;
 | 
					        exit = true;
 | 
				
			||||||
        break;
 | 
					        break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -60,7 +61,7 @@ int main (const int argc, const char * argv[]) {
 | 
				
			|||||||
        // Take input for previously encrypted message to decrypt
 | 
					        // Take input for previously encrypted message to decrypt
 | 
				
			||||||
//        result = cData.Decrypt();
 | 
					//        result = cData.Decrypt();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        std::cout << "Decrypted message: " << result << std::endl;
 | 
					        std::cout << "Decrypted message: \"" << result << "\"\n";
 | 
				
			||||||
        exit = true;
 | 
					        exit = true;
 | 
				
			||||||
        break;
 | 
					        break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -38,7 +38,7 @@ std::string CipherData::GetKey()
 | 
				
			|||||||
  std::string key;
 | 
					  std::string key;
 | 
				
			||||||
  std::cout << "Enter the keyword: ";
 | 
					  std::cout << "Enter the keyword: ";
 | 
				
			||||||
  std::getline(std::cin, key);
 | 
					  std::getline(std::cin, key);
 | 
				
			||||||
  std::cout << "Keyword entered: " << key << std::endl;
 | 
					  std::cout << "Keyword entered: \"" << key << "\"\n";
 | 
				
			||||||
  return key;
 | 
					  return key;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -61,6 +61,17 @@ void CipherData::Transpose(const std::vector<std::string> &in,
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void CipherData::ValidateKeyword(const std::string &message)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  if (keyWord_.size() < message.size()) return;
 | 
				
			||||||
 | 
					  // Pop letters from keyWord until it is < message.size()
 | 
				
			||||||
 | 
					  while (keyWord_.size() >= message.size()) keyWord_.pop_back();
 | 
				
			||||||
 | 
					  // Do not append order to a previous orderVect; Erase it first
 | 
				
			||||||
 | 
					  orderVect_.erase(orderVect_.begin(), orderVect_.end());
 | 
				
			||||||
 | 
					  // Reinitialize orderVect with a valid order for the new keyWord
 | 
				
			||||||
 | 
					  InitOrder(keyWord_);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
std::string CipherData::Encrypt(std::string message)
 | 
					std::string CipherData::Encrypt(std::string message)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
  // If no message was provided, ask for one
 | 
					  // If no message was provided, ask for one
 | 
				
			||||||
@ -69,6 +80,7 @@ std::string CipherData::Encrypt(std::string message)
 | 
				
			|||||||
    std::getline(std::cin, message);
 | 
					    std::getline(std::cin, message);
 | 
				
			||||||
    std::cout << "Encrypting message \"" << message << "\"\n";
 | 
					    std::cout << "Encrypting message \"" << message << "\"\n";
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					  ValidateKeyword(message);
 | 
				
			||||||
  std::string encryptedMessage;
 | 
					  std::string encryptedMessage;
 | 
				
			||||||
  std::vector<std::string> rows;
 | 
					  std::vector<std::string> rows;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -95,7 +107,8 @@ std::string CipherData::Decrypt(std::string message)
 | 
				
			|||||||
    std::getline(std::cin, message);
 | 
					    std::getline(std::cin, message);
 | 
				
			||||||
    std::cout << "Decrypting message \"" << message << "\"\n";
 | 
					    std::cout << "Decrypting message \"" << message << "\"\n";
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  std::string result;
 | 
					  ValidateKeyword(message);
 | 
				
			||||||
 | 
					  std::string decryptedMessage;
 | 
				
			||||||
  std::vector<std::string> rows;
 | 
					  std::vector<std::string> rows;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // Split the message into rows equal to the length of our keyWord
 | 
					  // Split the message into rows equal to the length of our keyWord
 | 
				
			||||||
@ -122,6 +135,6 @@ std::string CipherData::Decrypt(std::string message)
 | 
				
			|||||||
  std::vector<std::string> transposed;
 | 
					  std::vector<std::string> transposed;
 | 
				
			||||||
  Transpose(rows, transposed);
 | 
					  Transpose(rows, transposed);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  for (const auto &row : transposed) result += row;
 | 
					  for (const auto &row : transposed) decryptedMessage += row;
 | 
				
			||||||
  return  result;
 | 
					  return  decryptedMessage;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -41,6 +41,11 @@ struct CipherData {
 | 
				
			|||||||
  std::string Decrypt(std::string message="");
 | 
					  std::string Decrypt(std::string message="");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
private:
 | 
					private:
 | 
				
			||||||
 | 
					  // Used to make a valid keyword from an invalid keyWord
 | 
				
			||||||
 | 
					  // + Still encrypts / decrypts with whatever the user inputs as keyWord
 | 
				
			||||||
 | 
					  // + But technically the keyword is modified without the user knowing :)
 | 
				
			||||||
 | 
					  void ValidateKeyword(const std::string &message);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  std::string keyWord_;
 | 
					  std::string keyWord_;
 | 
				
			||||||
  std::vector<int> orderVect_;
 | 
					  std::vector<int> orderVect_;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user