0% found this document useful (0 votes)
13 views2 pages

Practical No.5 Program Code

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

Practical No.5 Program Code

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

PRACTICAL NO.

5
PROGRAM CODE:
#include <iostream>
#include <string>
using namespace std;
// Function to encrypt the message
string encrypt(string text, int shift) {
string result = "";

// Traverse each character in the message


for (int i = 0; i < text.length(); i++) {
// Encrypt uppercase letters
if (isupper(text[i])) {
result += char(int(text[i] + shift - 65) % 26 + 65);
}
// Encrypt lowercase letters
else if (islower(text[i])) {
result += char(int(text[i] + shift - 97) % 26 + 97);
}
else {
// If it's not a letter, leave it unchanged
result += text[i];
}
}
return result;
}

// Function to decrypt the message


string decrypt(string text, int shift) {
return encrypt(text, 26 - shift); // Decrypting is the reverse of encrypting
}

int main() {
string message;
int shift;

// Get user input for the message and shift value


cout << "Enter the message: ";
getline(cin, message);
cout << "Enter shift value (key): ";
cin >> shift;

// Ensure the shift is between 0 and 25


shift = shift % 26;

// Encrypt the message


string encryptedMessage = encrypt(message, shift);
cout << "Encrypted message: " << encryptedMessage << endl;

// Decrypt the message


string decryptedMessage = decrypt(encryptedMessage, shift);
cout << "Decrypted message: " << decryptedMessage << endl;

return 0;
}
OUTPUT:

You might also like