Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Caesar encryption program (code)

#include<iostream>
#include <string>
using namespace std;
int main()
{
char message[100];// string message;
char ch;
int i, key;
cout << "Enter a message to encrypt: ";
cin.getline(message, 100);// getline(cin, message);
cout << "Enter key: ";
cin >> key;
if (key<1)
{
cout<<"You have entered an invalid key. \n";
cout<<"Please try again. \n";
cout<<"Enter a proper key for letter shifting: ";
cin >> key;
}
for(i=0; message[i]!='\0'; ++i) //for(i=0;i<message.length();
++i)
{
ch = message[i];
if (ch >= 'a' && ch <= 'z')
{
ch = ch + key;
if (ch > 'z')
ch = ch - 26;
message[i] = ch;
}
else if (ch >= 'A' && ch <= 'Z')
{
ch = ch + key;
if (ch > 'Z')
ch = ch - 26;
message[i] = ch;
}
}
cout << "Encrypted message: " << message;
cout << endl;
system("pause");
return 0;

You might also like