Question

in C++, please show step by step with simple codes. thank you. An application from security:...

in C++, please show step by step with simple codes. thank you.

An application from security: Shift cyphers
A cypher is a method of encrypting data, where letters are encoded by replacing them with other letters in a known pattern. Shift cyphers are an important class of cyphers, where a numerical shift is chosen, e.g. 3, and all letters are replaced by the letter occurring that many spots (e.g. 3 spots) later in the alphabet. So if our shift was 3, the word “cat” would be encrypted as “fdw”
While doing this problem, it may be useful to refer to the following code:

s="heLlO"
for i=0, i<length(s), i++
if(s[i]>='A' and s[i] <='Z')
print("uppercase", s[i])
else:
print("lowercase", s[i])

1. Write pseudocode for a function string encrypt(string in, int shift) that returns the encrypted version of in using a shift of shift. So ecrypt(“cat”, 3) would return “fdw”. You MUST use character arithmetic – no built-in string functions besides length are allowed.
2. Turn your pseudocode from 1 into C++ code.

3. If you didn’t account for it in step 1, consider encoding the word “zoo” with a shift of 3. What would be the logical way to do this encoding? Change your pseudocode from 1 to account for this.

4. Turn your pseudocode from 3 into C++ code.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer 1:

Pseudocode for encrypt(string in, int shift)

1. declare a output string

2. for i <- 0 to i < in.length()

i. out[i] <- in[i] + shift

3. end for

4. return output string

Answer 2:

Code:

Code as text:

#include <iostream>

using namespace std;

/* function to encrypt string using shift */

string encrypt(string in, int shift) {

string out; // output string

out.resize(in.length());

// loop to shift whole string

for (int i = 0; i < in.length(); i++) {

out[i] = in[i] + shift;

}

return out; // return output string

}

int main() {

// tsting function

cout << encrypt("cat", 3) << endl;

return 0;

}

Sample Run:

Answer 3:

Pseudocode for encrypt(string in, int shift)

1. declare a output string

2. for i <- 0 to i < in.length()

i. out[i] <- in[i] + shift

ii. check if out[i] is in alphabet range, if not then adjust

a. out[i] <- out[i] - 26

ii. end if

3. end for

4. return output string

Answer 4:

Code:

Code as text:

#include <iostream>

using namespace std;

/* function to encrypt string using shift */

string encrypt(string in, int shift) {

string out; // output string

out.resize(in.length());

// loop to shift whole string

for (int i = 0; i < in.length(); i++) {

out[i] = in[i] + shift;

// adjust shifting

if (in[i] >= 'a' && in[i] <= 'z' && out[i] > 'z')

out[i] -= 26;

if (in[i] >= 'A' && in[i] <= 'Z' && out[i] > 'Z')

out[i] -= 26;

}

return out; // return output string

}

int main() {

// tsting function

cout << "cat: " << encrypt("cat", 3) << endl;

cout << "zoo: " << encrypt("zoo", 3) << endl;

return 0;

}

Sample run:

P.s. ask any doubts in comments and don't forget to rate the answer.

Add a comment
Know the answer?
Add Answer to:
in C++, please show step by step with simple codes. thank you. An application from security:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT