Guest User

Untitled

a guest
Jan 18th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. if (_user_text[i] == _alphabet[j])
  2.  
  3. void uppgift_2() {
  4.  
  5. // Uppgift:
  6. // Du skall skapa en enkel kryptering. Låt användaren mata in en text av typen string, valfri storlek.
  7. // Skriv sedan ut en krypterad version av strängen med rot3.
  8.  
  9. // Array with the alphabet in it
  10. char alphabet[36] = { 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
  11. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
  12.  
  13. // Variables
  14. std::string user_text = "", org_string = "";
  15. std::string temp = "";
  16. int rot_x = 7; // rot7
  17.  
  18. // Prompt user for input
  19. std::cout << "| Enter in a random text (english): ";
  20. getline(std::cin, user_text);
  21. org_string = user_text;
  22.  
  23. // Make string lowercase
  24. for (unsigned int b = 0; b < user_text.length(); b++) {
  25. char lower_Version = tolower(user_text[b]);
  26. temp += lower_Version;
  27. }
  28. user_text = temp;
  29.  
  30. // Encrypting user entered text...
  31. std::string _encrypted_text = encrypt_string(user_text, alphabet, rot_x);
  32.  
  33. // Prints out the original string, the encrypted version and the final decrypted version
  34. std::cout << "n| Original version: " << org_string << std::endl;
  35. std::cout << "| - - - - - - - - - - - - - - - - - - - - - - - - - - - |n" << std::endl;
  36. std::cout << "| Encrypted version: " << _encrypted_text << std::endl;
  37. std::cout << "| Decrypted version: " << decrypt_string(_encrypted_text, alphabet, rot_x) << "nn";
  38.  
  39. system("PAUSE");
  40. main_menu(); // Sends the user back to the main menu
  41.  
  42. }
  43.  
  44. std::string encrypt_string(std::string &_user_text, char(&_alphabet)[36], int &rot_x) {
  45.  
  46. // Variable(s)
  47. std::string encrypted_text = "";
  48. int array_len = 35;
  49. int pointer = 0; // Used to determine if a switch from rot7 to rot13 is needed
  50.  
  51. // The outer loop goes through each character of the user entered string
  52. for (int i = 0; encrypted_text.length() < _user_text.length(); i++) {
  53. // The inner loop finds the correspondent character from the alphabet and uses rot7 or rot13 on it
  54. for (int j = 0; j <= array_len; j++) {
  55. // if (selected character from user string) is equal to (the correspondant character in the alphabet)
  56. if (_user_text[i] == _alphabet[j]) {
  57. int alphabet_index = (j + rot_x);
  58. // Checks if alphabet_index is out of range and adjusts accordingly
  59. if (alphabet_index >= array_len+1) {
  60. alphabet_index -= array_len+1;
  61. }
  62. // Saves the decrypted character...
  63. char encrypted_char = _alphabet[alphabet_index];
  64. // ... and concatenates it to the decrypted string
  65. encrypted_text += encrypted_char;
  66. break; // Breaks out of inner loop and lets outer loop get the next character
  67. }
  68. }
  69. pointer++;
  70. // If five characters have been encrypted, switch to rot13
  71. if (pointer == 5) {
  72. rot_x = 13;
  73. }
  74. // Go back to rot7 when yet another 5 characters have been encrypted
  75. else if (pointer == 10) {
  76. rot_x = 7;
  77. pointer = 0; // Reset the pointer
  78. }
  79. }
  80.  
  81. // Returns the final encrypted text
  82. return encrypted_text;
  83.  
  84. }
Add Comment
Please, Sign In to add comment