0% found this document useful (0 votes)
5 views4 pages

Task 1

Uploaded by

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

Task 1

Uploaded by

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

QUESTION NO 1_

IMPLEMENT CEASER CIPHER USING C++.

#include <iostream>

#include <string>

using namespace std;

string caesarCipher(const string& text, int shift) {

string result = "";

shift = shift % 26;

for (char ch : text) {

if (isalpha(ch)) {

char base = isupper(ch) ? 'A' : 'a';

result += char(int(ch - base + shift + 26) % 26 + base);

} else {

result += ch;

return result;

int main() {

string text;

int shift;

char choice;
cout << "Enter text: ";

getline(cin, text);

cout << "Enter shift value (0-25): ";

cin >> shift;

cout << "Do you want to (E)ncrypt or (D)ecrypt? ";

cin >> choice;

if (choice == 'E' || choice == 'e') {

string encryptedText = caesarCipher(text, shift);

cout << "Encrypted Text: " << encryptedText << endl;

} else if (choice == 'D' || choice == 'd') {

string decryptedText = caesarCipher(text, -shift);

cout << "Decrypted Text: " << decryptedText << endl;

} else {

cout << "Invalid choice!" << endl;

return 0;

You might also like