lab1
#include <iostream>
#include <string>
using namespace std;
string caesarEncrypt(string text, int shi ) {
string result = "";
for (char c : text) {
if (isalpha(c)) {
char base = isupper(c) ? 'A' : 'a';
result += (char)((c - base + shi ) % 26 + base);
} else {
result += c;
}
}
return result;
}
string caesarDecrypt(string text, int shi ) {
return caesarEncrypt(text, 26 - (shi % 26));
}
int main() {
string inputText;
int shi ;
int choice;
string encrypted, decrypted;
cout << "=== Caesar Cipher ===\n";
cout << "Enter the text: ";
getline(cin, inputText);
cout << "Enter the shi value: ";
cin >> shi ;
encrypted = caesarEncrypt(inputText, shi );
cout << "Encrypted Text: " << encrypted << endl;
decrypted = caesarDecrypt(encrypted, shi );
cout << "Decrypted Text: " << decrypted << endl;
return 0;
}
Comments
Post a Comment