Write a program that can \"encrypt\" a word using a Caesar cipher (shifting a ch
ID: 3632111 • Letter: W
Question
Write a program that can "encrypt" a word using a Caesar cipher (shifting a character K distance away). You are essentially manipulating the ASCII value for each character. To "encrypt", simply just add K to the character value. Restrict the valid range to be human readable characters (i.e. ASCII values between 32 and 126, inclusively). The program should allow the user to "encrypt" multiple words.Example:
Cryptography program!!!
Input a shifting distance that's less than 10: 2
Input a word: Hello
Encrypted word is:
Jgnnq
Do you still want to continue?[y/n]: y
Input a word: World
Encrypted word is:
Yqtnf
Do you still want to continue?[y/n]: N
Explanation / Answer
//header files
#include<iostream>
#include<string>
using namespace std;
//main function
int main()
{
//variable declaration
int distance,i,len;
char choice;
string word;
//prompting user for input
cout<<"Cryptography program!!!"<<endl;
cout<<"Input a shifting distance that's less than 10: ";
cin>>distance;//reading key
do
{
//reading word.
cout<<"Input a word: ";
cin>>word;
i=0;
len = word.length();
//encrypting
while(i<len)
{
word[i]=word[i]+distance;
i++;
}
//displaying encrypted word
cout<<"Encrypted word is:"<<endl;
cout<<word<<endl;
//ask for continuing
cout<<"Do you still want to continue?[y/n]: ";
cin>>choice;
choice=tolower(choice);
}while(choice=='y');
system("pause");
return 0;
}//end of main
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.