Answer to Question #208951 in Python for python

Question #208951

write a program that contains two functions i.e., encrypt_string() and decrypt_string()a.encrypt_string(s): Thisfunction uses the following dictionary:encrypt={'a':'1','b':'@','c':'*','d':')','e':'$','f':'#'}This function should accept a string as its parameter and return anencrypted string. To encrypt a string, use the dictionary given above. Read each character of the string(match it with the keyin the dictionary)and replace each character with the given encrypted value(valuein the dictionary)and return the encrypted string.b.decrypt_string(s)This function uses the following dictionary:decrypt={'1':'a','@':'b','*':'c',')':'d','$':'e','#':'f'}This function should accept an encrypted string as its parameter and return the decrypted/original string. To decrypt a string, use the dictionary given above. Read each character of the string(match it with the key in the dictionary) andreplace each character with the given decrypted value(value in the dictionary)and return the decrypted string.


1
Expert's answer
2021-06-20T08:11:53-0400
def encrypt_string(word: str):
    res = ''
    for i in range(len(word)):
        if word[i] == 'a':
            res += '1'
        elif word[i] == 'b':
            res += '@'
        elif word[i] == 'c':
            res += '*'
        elif word[i] == 'd':
            res += ')'
        elif word[i] == 'e':
            res += '$'
        elif word[i] == 'f':
            res += '#'
        else:
            res += word[i]
    return res;
    
def decrypt_string(word: str):
    res = ''
    for i in range(len(word)):
        if word[i] == '1':
            res += 'a'
        elif word[i] == '@':
            res += 'b'
        elif word[i] == '*':
            res += 'c'
        elif word[i] == ')':
            res += 'd'
        elif word[i] == '$':
            res += 'e'
        elif word[i] == '#':
            res += 'f'
        else:
            res += word[i]
    return res;
    
if __name__ == '__main__':
    s = input('Enter some word:')
    print('---')
    s = encrypt_string(s)
    print(s)
    s = decrypt_string(s)
    print(s)
    print('---')

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS