Advertisement
Cinestra

Untitled

Jan 4th, 2023
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. // Decode.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2.  
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. string encrypt(const string& s, int n) {
  7. string new_string = "";
  8.  
  9. for (int i = 0; i < s.length(); i++)
  10. {
  11. if (s[i] == ' ')
  12. {
  13. new_string += ' ';
  14. continue;
  15. }
  16.  
  17. int how_far_away = s[i] - 'a';
  18. int new_character = (how_far_away + n) % 25;
  19. new_string += 'a' + new_character;
  20. }
  21.  
  22. return new_string;
  23.  
  24. }
  25.  
  26. string remove_first_word(const string& s) {
  27. string new_string = "";
  28.  
  29. for (int i = 0; i < s.length(); i++)
  30. {
  31.  
  32. if (s[i] == ' ')
  33. {
  34. for (int j = i + 1; j < s.length(); j++) {
  35. // Start one position to the right of the space
  36. new_string += s[j];
  37. }
  38.  
  39. return new_string;
  40. }
  41. }
  42.  
  43. return new_string; //returns empty if there is only one word in the string
  44. }
  45.  
  46. string first_word(const string& s) {
  47. string new_string = "";
  48.  
  49. for (int i = 0; i < s.length(); i++)
  50. {
  51. if (s[i] != ' ')
  52. {
  53. new_string += s[i];
  54. }
  55.  
  56. else
  57. {
  58. return new_string;
  59. }
  60. }
  61.  
  62. return new_string; //Returns if this is the last word in the string
  63. }
  64.  
  65. bool check_word(string s, const string& key)
  66. {
  67. if (s.length() != key.length()) //First check the if the lengths of the strings even match
  68. {
  69. return false;
  70. }
  71.  
  72. for (int i = 0; i < s.length(); i++) //Then check each character
  73. {
  74. if (s[i] != key[i])
  75. {
  76. return false;
  77. }
  78. }
  79.  
  80. return true;
  81. }
  82.  
  83. string decode(const string& code, const string& key)
  84. {
  85. for (int i = 0; i < 26; i++)
  86. {
  87. string decoded_string = encrypt(code, i);
  88. string dummy_string = decoded_string; // We need a copy string that we can remove words from
  89. string decoded_word = first_word(dummy_string);
  90.  
  91. while (!check_word(decoded_word, key) && (decoded_word.length() > 0)) //Remove_first_word keeps getting rid of the first word so if it isn't the right key, it will return a null string-- so must account for that
  92. {
  93. dummy_string = remove_first_word(dummy_string);
  94. decoded_word = first_word(dummy_string);
  95. }
  96.  
  97. if (check_word(decoded_word, key))
  98. {
  99. return decoded_string;
  100. }
  101. }
  102. }
  103.  
  104. int main()
  105. {
  106. string s = "YoonA eats";
  107. string x = "YoonA";
  108. string y = remove_first_word(s);
  109. string z = first_word(s);
  110. cout << y << endl;
  111. cout << z << endl;
  112.  
  113. string encoded = "ojdl jt tnbsu jg if dbo tpmwf uijt";
  114. cout << encoded << endl;
  115. string decoded = decode(encoded, "smart");
  116. cout << encoded << endl;
  117. cout << decoded << endl; // prints "nick is smart if he can solve this"
  118.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement