Answer to Question #223967 in Python for anand

Question #223967
Replacing Characters of Sentence

You are given a string 

S. Write a program to replace each letter of the string with the next letter that comes in the English alphabet.

Note: Ensure that while replacing the letters, uppercase should be replaced with uppercase letters, and lowercase should be replaced with lowercase letters.

Input

The first line of input is a string.

Explanation

In the given example, 

Hello World.

If we replace each letter with the letter that comes next in the English alphabet, 

H becomes I, e becomes f and so on ... So, the output should be Ifmmp Xpsme.

Sample Input 1
Hello World
Sample Output 1
Ifmmp Xpsme
Sample Input 2
Something is better than Nothing
Sample Output 2
Tpnfuijoh jt cfuufs uibo Opuijoh

1
Expert's answer
2021-08-09T02:35:22-0400
# Python 3.9.5
def enter_phrase():
    phrase = input('Enter phrase: ')
    return phrase

def capitalize_symbol(phrase):
    k = ""
    for i in phrase:
        if i != ' ':
            if i.istitle():
                next = chr(97 if i == 'z' else ord(i)+1)
                if next in ('a', 'e', 'i', 'o', 'u'):
                    next = next.capitalize()
                k += next.title()
            else:
                next = chr(97 if i == 'z' else ord(i) + 1)
                if next in ('a', 'e', 'i', 'o', 'u'):
                    next = next.capitalize()
                k += next.lower()
        else:
            k += ' '
    return k

def main():
    phrase = enter_phrase()
    print(capitalize_symbol(phrase))

if __name__ == '__main__':
    main()

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