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:
Shaun Reed 2021-07-20 16:04:18 -04:00
parent c8683680dd
commit 909bf3278e
3 changed files with 25 additions and 6 deletions

View File

@ -23,6 +23,7 @@
// Main program loop
int main (const int argc, const char * argv[]) {
// 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
// + 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
// result = cData.Encrypt();
std::cout << "Encrypted message: " << result << std::endl;
std::cout << "Encrypted message: \"" << result << "\"\n";
exit = true;
break;
@ -60,7 +61,7 @@ int main (const int argc, const char * argv[]) {
// Take input for previously encrypted message to decrypt
// result = cData.Decrypt();
std::cout << "Decrypted message: " << result << std::endl;
std::cout << "Decrypted message: \"" << result << "\"\n";
exit = true;
break;

View File

@ -38,7 +38,7 @@ std::string CipherData::GetKey()
std::string key;
std::cout << "Enter the keyword: ";
std::getline(std::cin, key);
std::cout << "Keyword entered: " << key << std::endl;
std::cout << "Keyword entered: \"" << key << "\"\n";
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)
{
// 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::cout << "Encrypting message \"" << message << "\"\n";
}
ValidateKeyword(message);
std::string encryptedMessage;
std::vector<std::string> rows;
@ -95,7 +107,8 @@ std::string CipherData::Decrypt(std::string message)
std::getline(std::cin, message);
std::cout << "Decrypting message \"" << message << "\"\n";
}
std::string result;
ValidateKeyword(message);
std::string decryptedMessage;
std::vector<std::string> rows;
// 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;
Transpose(rows, transposed);
for (const auto &row : transposed) result += row;
return result;
for (const auto &row : transposed) decryptedMessage += row;
return decryptedMessage;
}

View File

@ -41,6 +41,11 @@ struct CipherData {
std::string Decrypt(std::string message="");
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::vector<int> orderVect_;
};