0

So I am trying to replace a character in a string with the next letter of the alphabet using strings. I have got the input converted but I don't know what to do from here? Here is my code:

alphabet =['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
wordlist = []
inputword = input("Please enter a string")
wordlist = list(inputword)
for i in range(len(inputword)):

print (wordlist)

I am trying to get it so that if I inputted "Hello World" , it will return "Ifmmp xpsme"

It doesn't have to return the exact case (upper case letters can be returned as lower case

1 Answers1

0

If I understood correctly, you want to replace a character that is given by a user with the next character of the alphabet.

Assuming that z should become a use this:

For single character inputs (e.g. 'a') use this:

alphabet =['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
wordlist = []
inputword = input("Please enter a string:")
wordlist = list(inputword)

if wordlist[0] == 'z':
    #print('Sorry, I do not know what to do for this case')
    idx = 0
    new_word = alphabet[idx]
    print(new_word)

else:
    idx = alphabet.index(wordlist[0])
    new_word = alphabet[idx +1]
    print(new_word)

For whole string inputs (e.g. 'hello world') use this:

def next_letter(st):
    index = 0
    new_word = ""
    alphapet = "abcdefghijklmnopqrstuvwxyzacd"

    for i in st.lower():
        if i.islower(): #check if i s letter
            index = alphapet.index(i) + 1 #get the index of the following letter
            new_word += alphapet[index]    
        else: #if not letter
            new_word += i    
    return new_word

USER_INPUT = input('enter string:')

next_letter(USER_INPUT)

Example:

USER_INPUT= 'hello world'

print(next_letter(USER_INPUT))
'ifmmp xpsme'
seralouk
  • 30,938
  • 9
  • 118
  • 133