Advertisement
Cinestra

Untitled

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