Projects Inventory

Caesar Cipher Gaming Project in C++

Explanation

  1. Include Libraries:
    • #include <iostream>: For input and output operations.
    • #include <string>: For using std::string.
  2. Encrypt Function:
    • string encrypt(string plaintext, int shift)
      Advertisement
      : Encrypts the plaintext using the Caesar cipher.
      • plaintext: The input string to be encrypted.
      • shift: The number of positions each letter in the plaintext is shifted.
      • Iterates over each character in plaintext:
        • Checks if the character is a letter (isalpha(c)
          Advertisement
          ).
        • Determines if it is uppercase or lowercase to set the base ('A' or 'a').
        • Applies the shift using modular arithmetic to ensure the result wraps around the alphabet.
        • Appends the encrypted character to ciphertext.
        • Non-alphabetic characters are added to ciphertext unchanged.
  3. Decrypt Function:
    • string decrypt(string ciphertext, int shift): Decrypts the ciphertext by using the inverse shift.
      • Calls the encrypt function with 26 - shift, as decryption in Caesar cipher is essentially shifting backwards by the same amount.
  4. Main Function (main):
    • Prompts the user to enter text and shift value.
    • Validates that the shift value is between 1 and 25.
    • Encrypts the input text using the encrypt
      Advertisement
      function and prints the result.
    • Decrypts the encrypted text using the decrypt function and prints the result.

Notes:

Exit mobile version