11:06 LTE Module 7 Lab 5 Detail Grade A Caesar cipher encrypts a message by shif
ID: 3920211 • Letter: 1
Question
11:06 LTE Module 7 Lab 5 Detail Grade A Caesar cipher encrypts a message by shifting all letters in the message a certain number of positions down the alphabet (wrapping around if necessary). It should also put the letters in groups of five. For example, if the string is "the quick brown foxes" and the offset number is 4, then we get "xliuy mgofv sarjsb iw". Write a function that takes as parameters a string and an offset and returns the encrypted string. You can ass string consists only of lower-case letters and spaces. (See if you can avoid using ASCII values in your code - add a number to a character and compare whether a character is less than another character.) ume the you can do things like Write a function that takes a vector of string objects and replaces each string in the vector with its encrypted form (using the above function). This call should change the original vector, not just a copy. Remember that it's better to use at0 than the array square-bracket notation. Previous Next Calendar To Do InboxExplanation / Answer
The code for Ciphering the Strings Object and the Demonstration.Comments are added where necessary.The offset is by default set to 4 .If you want you can change it to some other value less than 26(as the characters are 26) . You can also add one parameter into Change_vector() function for offset by User input.
//Cipher.cpp
#include<iostream>
#include<string>
#include<vector>
using namespace std;
string Get_Cipher
(
string input , //Input String
int offset //InputOffset
) //Function Which Ciphers the String.
{
string output=""; //Output String
char ch;
for(int i=0;i<input.size();i++)
{
ch=input.at(i); //Getting single character of String into ch
ch='a'+(ch-'a'+offset)%26;//Actual Code to increment character by offset
output+=ch; //putting character into output string
if((i+1)%5==0) //grouping by 5
{
output+=' ';
}
}
return output;//returning the Output
}
void Change_vector( //Function for getting pointer to the String vector for Ciphering
vector <string> * vtrptr//pointer to vector to replace the original
)
{
for(int i=0;i<vtrptr->size();i++)
{
vtrptr->at(i)=Get_Cipher(vtrptr->at(i),4);/*Accessing element of vector one by one and sending to function
here the Elements are replaced by ciphered string*/
cout<<vtrptr->at(i)<<" ";//Printing the Converted strings
}
}
int main()//For demonstrating the functions
{
vector <string> vectStr;//Demo vector of string objects
vectStr.push_back("cpp");//inserting string object at the end of vector
vectStr.push_back("java");//inserting another string object in end of vector
Change_vector(&vectStr);//function call
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.