60.9k views
1 vote
Implement function easyCrypto() that takes as input a string and prints its encryption

defined as follows: Every character at an odd position i in the alphabet will be
encrypted with the character at position i+1, and every character at an even position i
will be encrypted with the character at position i 1. In other words, ‘a’ is encrypted with
‘b’, ‘b’ with ‘a’, ‘c’ with ‘d’, ‘d’ with ‘c’, and so on. Lowercase characters should remain
lowercase, and uppercase characters should remain uppercase.
>>> easyCrypto('abc')
bad
>>> easyCrypto('Z00')
YPP

User Nfvindaloo
by
5.0k points

1 Answer

4 votes

Answer:

The program written in python is as follows;

import string

def easyCrypto(inputstring):

for i in range(len(inputstring)):

try:

ind = string.ascii_lowercase.index(inputstring[i])

pos = ind+1

if pos%2 == 0:

print(string.ascii_lowercase[ind-1],end="")

else:

print(string.ascii_lowercase[ind+1],end="")

except:

ind = string.ascii_uppercase.index(inputstring[i])

pos = ind+1

if pos%2 == 0:

print(string.ascii_uppercase[ind-1],end="")

else:

print(string.ascii_uppercase[ind+1],end="")

anystring = input("Enter a string: ")

easyCrypto(anystring)

Step-by-step explanation:

The first line imports the string module into the program

import string

The functipn easyCrypto() starts here

def easyCrypto(inputstring):

This line iterates through each character in the input string

for i in range(len(inputstring)):

The try except handles the error in the program

try:

This line gets the index of the current character (lower case)

ind = string.ascii_lowercase.index(inputstring[i])

This line adds 1 to the index

pos = ind+1

This line checks if the character is at even position

if pos%2 == 0:

If yes, it returns the alphabet before it

print(string.ascii_lowercase[ind-1],end="")

else:

It returns the alphabet after it, if otherwise

print(string.ascii_lowercase[ind+1],end="")

The except block does the same thing as the try block, but it handles uppercase letters

except:

ind = string.ascii_uppercase.index(inputstring[i])

pos = ind+1

if pos%2 == 0:

print(string.ascii_uppercase[ind-1],end="")

else:

print(string.ascii_uppercase[ind+1],end="")

The main starts here

This line prompts user for input

anystring = input("Enter a string: ")

This line calls the easyCrypto() function

easyCrypto(anystring)

User Confiance
by
5.8k points