1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
ASHA 777 [7]
3 years ago
8

Program a substitution cipher in Python. Create 2 functions one for encryption and the other for decryption ( get the text as th

e parameter).
Computers and Technology
1 answer:
raketka [301]3 years ago
7 0

alphabet = "abcdefghijklmnopqrstuvwxyz"

def encrypt(txt):

   shift = int(input("Enter the shift: "))

   new_txt = ""

   for x in txt:

       if x in alphabet:

           if alphabet.index(x)+shift >= len(alphabet):

               new_txt += alphabet[(alphabet.index(x)+shift - len(alphabet))]

           else:

               new_txt += alphabet[alphabet.index(x)+shift]

       else:

           new_txt += x

   return new_txt

def decrpyt(txt):

   shift = int(input("Enter the known shift: "))

   new_txt = ""

   for x in txt:

       if x in alphabet:

           new_txt += alphabet[alphabet.index(x) - shift]

       else:

           new_txt += x

   return new_txt

print(encrypt("yellow dog."))

print(decrpyt("lryybj qbt."))

My code works with lowercase text only. If you need me to modify the code, I can do that. I tested my code with a shift of 13 in both the encryption and decryption. I hope this helps!

You might be interested in
Develop an algorithm and write a C++ program that finds the number of occurrences of a specified character in the string; make s
nirvana33 [79]

Answer:

The algorithm is as follows:

1. Input sentence

2. Input character

3. Length = len(sentence)

4. count = 0

5. For i = 0 to Length-1

 5.1 If sentence[i] == character

    5.1.1 count++

6. Print count

7. Stop

The program in C++ is as follows:

#include <iostream>

#include <string.h>

using namespace std;

int main(){

char str[100];   char chr;

cin.getline(str, 100);

cin>>chr;

int count = 0;

for (int i=0;i<strlen(str);i++){

 if (str[i] == chr){

  count++;} }

cout << count;

return 0;}

Explanation:

I will provide explanation for the c++ program. The explanation can also be extended to the algorithm

This declares the string and the character

char str[100];   char chr;

This gets input for the string variable

cin.getline(str, 100);

This gets input for the character variable

cin>>chr;

This initializes count to 0

int count = 0;

This iterates through the characters of the string

for (int i=0;i<strlen(str);i++){

If the current character equals the search character, count is incremented by 1

 if (str[i] == chr){  count++;} }

This prints the count

cout << count;

8 0
3 years ago
If you were a game programer (NOT using scratch or other basic software making platforms) what kind of code would you use for th
natulia [17]
8bitworkshop.com. It wouldn’t give you like an undertale thing, but its still super 80s gaming aesthetic
3 0
3 years ago
Read 2 more answers
When comparison shopping, all of these hint at a good deal EXCEPT_____________________.
noname [10]

Answer:

lower-priced models offer more features

Explanation:

3 0
3 years ago
Guys please inbox me​
ololo11 [35]

Answer:

how do i do that?

Explanation:

5 0
3 years ago
Read 2 more answers
Select all the correct answers.
monitta

approvals necessary to bypass the policy

6 0
3 years ago
Read 2 more answers
Other questions:
  • Perception is a simple process.
    11·2 answers
  • Assume you had a .csv file with data already populated that you wanted to use for a mail merge. Which button would you press to
    8·1 answer
  • An increase in pay because of how well you do a job is called _____.
    9·2 answers
  • Luchen Modo, a software development firm in Asia, launched a new software for smartphones that allowed users to remotely control
    11·1 answer
  • Using truth table, prove that:<br><br> (A + B). C = (A . C)+ (B .C) ?
    7·1 answer
  • Assume that word is a variable of type string that has been assigned a value. Write an expression whose value is a string consis
    14·1 answer
  • What is the last usable host IP address on the 192.168.32.9/30 network?
    15·1 answer
  • Need the answer ASAP!!! I’ll mark brainliest if correct
    12·2 answers
  • What does this comparison block indicate?
    13·1 answer
  • When you try to move the desktop icon using the click and drag method and it doesn't move, what is the reason?​
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!