Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if (_user_text[i] == _alphabet[j])
- void uppgift_2() {
- // Uppgift:
- // Du skall skapa en enkel kryptering. Låt användaren mata in en text av typen string, valfri storlek.
- // Skriv sedan ut en krypterad version av strängen med rot3.
- // Array with the alphabet in it
- 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',
- '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
- // Variables
- std::string user_text = "", org_string = "";
- std::string temp = "";
- int rot_x = 7; // rot7
- // Prompt user for input
- std::cout << "| Enter in a random text (english): ";
- getline(std::cin, user_text);
- org_string = user_text;
- // Make string lowercase
- for (unsigned int b = 0; b < user_text.length(); b++) {
- char lower_Version = tolower(user_text[b]);
- temp += lower_Version;
- }
- user_text = temp;
- // Encrypting user entered text...
- std::string _encrypted_text = encrypt_string(user_text, alphabet, rot_x);
- // Prints out the original string, the encrypted version and the final decrypted version
- std::cout << "n| Original version: " << org_string << std::endl;
- std::cout << "| - - - - - - - - - - - - - - - - - - - - - - - - - - - |n" << std::endl;
- std::cout << "| Encrypted version: " << _encrypted_text << std::endl;
- std::cout << "| Decrypted version: " << decrypt_string(_encrypted_text, alphabet, rot_x) << "nn";
- system("PAUSE");
- main_menu(); // Sends the user back to the main menu
- }
- std::string encrypt_string(std::string &_user_text, char(&_alphabet)[36], int &rot_x) {
- // Variable(s)
- std::string encrypted_text = "";
- int array_len = 35;
- int pointer = 0; // Used to determine if a switch from rot7 to rot13 is needed
- // The outer loop goes through each character of the user entered string
- for (int i = 0; encrypted_text.length() < _user_text.length(); i++) {
- // The inner loop finds the correspondent character from the alphabet and uses rot7 or rot13 on it
- for (int j = 0; j <= array_len; j++) {
- // if (selected character from user string) is equal to (the correspondant character in the alphabet)
- if (_user_text[i] == _alphabet[j]) {
- int alphabet_index = (j + rot_x);
- // Checks if alphabet_index is out of range and adjusts accordingly
- if (alphabet_index >= array_len+1) {
- alphabet_index -= array_len+1;
- }
- // Saves the decrypted character...
- char encrypted_char = _alphabet[alphabet_index];
- // ... and concatenates it to the decrypted string
- encrypted_text += encrypted_char;
- break; // Breaks out of inner loop and lets outer loop get the next character
- }
- }
- pointer++;
- // If five characters have been encrypted, switch to rot13
- if (pointer == 5) {
- rot_x = 13;
- }
- // Go back to rot7 when yet another 5 characters have been encrypted
- else if (pointer == 10) {
- rot_x = 7;
- pointer = 0; // Reset the pointer
- }
- }
- // Returns the final encrypted text
- return encrypted_text;
- }
Add Comment
Please, Sign In to add comment