Write in C++: string encode (string message, string key) • converts message usin
ID: 3798314 • Letter: W
Question
Write in C++:
string encode (string message, string key)
• converts message using lower_and_strip
o key is not changed, but is used as is!
• for every character in the message
o call return_encoded_char with that character
o concatenates the number into a space separated string
• returns the encoded message
Given Code:
string lower_and_strip (string s) {
std::transform(s.begin(), s.end(), s.begin(), tolower);
s.erase (std::remove_if(s.begin(), s.end(), &isdigit), s.end());
s.erase (std::remove_if(s.begin(), s.end(), &ispunct), s.end());
return s;
}
int return_encoded_char(string key, string::size_type &start, char C)
{
int location = 0;
int begin_dist = 0;
for(string::size_type i = 0; i < key.size(); i++){
if( key[i] == C) {
if (start > 0) {
location = 26 - (start - i);
start = i;
}
else {
start = i;
location = i;
}
return location;
}
begin_dist += 1;
}
start = 0;
return key.size() + 1;
}
Sample Case:
abcdefghijklmnopqrstuvwxyz (this is the key)
Explanation / Answer
The code for the same is:
Source Code:
string encode (string message, string key)
{
//converts message using lower_and_strip
// you can use the same variable again if you don't want to declare new variable and make program space-efficient
message = lower_and_strip(message);
//key is not changed, but is used as is!
string enc_msg = NULL;
int num;
//for every character in the message
for(int i=0; message[i]!=NULL; i++)
{
// call return_encoded_char with that character
num = return_encoded_char(key, message.size(), message[i]);
// concatenate the number into a space separated string
enc_msg = enc_msg.append(""+num);
enc_msg = enc_msg.append(" ");
}
//return the encoded message
return enc_msg;
}
string lower_and_strip (string s) {
std::transform(s.begin(), s.end(), s.begin(), tolower);
s.erase (std::remove_if(s.begin(), s.end(), &isdigit), s.end());
s.erase (std::remove_if(s.begin(), s.end(), &ispunct), s.end());
return s;
}
int return_encoded_char(string key, string::size_type &start, char C)
{
int location = 0;
int begin_dist = 0;
for(string::size_type i = 0; i < key.size(); i++){
if( key[i] == C) {
if (start > 0) {
location = 26 - (start - i);
start = i;
}
else {
start = i;
location = i;
}
return location;
}
begin_dist += 1;
}
start = 0;
return key.size() + 1;
}
Description:
Here, I am supposed to implement the method encode that encodes the message string to integers using key.
I have written the code as per the comments itself step-by-step.
So, first you are supposed to call the lower_and_strip() method with message as argument that returns the string after performing various operations.
This function returns string. So, if you don't want to use other string variable, you can use the message variable itself.
After that, the key is not modified it is left as it is.
Now, I have declared a variable enc_msg that stores the encoded string and a temporary variable num which is used to form this string.
As per described, for every character in 'message' i.e. till we get NULL as a character which means the string is ended, I have called the method return_encoded_char() for every character.
This method is taking 3 parameters, the very first one is key which I already have in 'key' variable in the prototype of encode().The third one is a character to be encoded.
Second parameter is something you need to think of. This parameter is a string length. string::size_type is used to ensure that it can hold the string size whatever big it is. If you see the implementation of the return_encoded_char() method, it is using the key.size() in 1 for loop. So, that means you are not passing it to the function, and as per the definition of return_encoded_char(), the second parameter is string length. And we are working with only 2 strings here. That means you should pass the length of message in the return_encoded_char() as the second parameter. So I have written like that. After that, I have appended the integer stored in num to enc_msg. enc_msg was declared empty. It is appended with a number and after that a space. So, it becomes space separated string containing encoded message which is returned. (NOTE: num can't be appended directly as it is integer but ""+num can be appended as it is string.)
I have implemented as much is written here. I had tested for compilation error and there is no such error so you can check this code with your driver program.
Do comment if there is any query. Thank you. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.