Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int encryptioncheck(string koodi){
  6. string::size_type pituus = 0;
  7. pituus = koodi.length();
  8. if (pituus != 26){
  9. cout << "Error! The encryption key must contain 26 characters." << endl;
  10. return EXIT_FAILURE;
  11. }
  12.  
  13.  
  14.  
  15. std::locale loc;
  16. std::string str=koodi;
  17.  
  18. for (std::string::iterator it=str.begin(); it!=str.end(); ++it)
  19. {
  20. if (std::isalpha(*it,loc) and std::islower(*it,loc))
  21. continue;
  22. else
  23. std::cout << "Error! The encryption key must contain only lower case characters." << endl;
  24. return EXIT_FAILURE;
  25. }
  26. for (char a = 'a'; a < 'z'; a++) {
  27. if (koodi.find(a) == std::string::npos) {
  28. std::cout << "Error! The encryption key must contain all alphabets a-z." << std::endl;
  29. return EXIT_FAILURE;
  30. }
  31. }
  32.  
  33.  
  34.  
  35. return 0;
  36. }
  37.  
  38. char encrypt(string encryption, char c) {
  39. string alphabet = "abcdefghijklmnopqrstuvwxyz";
  40. string::size_type encrypted = alphabet.find(c);
  41. return encryption[encrypted];
  42. }
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49. int main()
  50. {
  51. string encryption_key = " ";
  52. string encrypted = "";
  53. cout << "Enter the encryption key: ";
  54. cin >> encryption_key;
  55. if (encryptioncheck(encryption_key) == EXIT_FAILURE){
  56. return EXIT_FAILURE;
  57. }
  58. else {
  59. string text = " ";
  60. cout << "Enter the text to be encrypted: ";
  61. cin >> text;
  62. for (char& c : text) {
  63. encrypted += encrypt(encryption_key, c);
  64. }
  65. cout << "Encrypted text: " << encrypted << endl;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement