I've written a function that takes a message (string) as an input and replaces the first letter of each word with the first letter of the previous word (for the very first word, I take the first letter of the last word):
def changeFirst(message):
msg_list=list(message)
j=0
pre=msg_list[j]
for i in range(len(msg_list)):
if msg_list[i]==' ':
nextpre=msg_list[i+1]
msg_list[i+1]=pre
pre=nextpre
msg_list[0]=pre
msg_list=''.join(msg_list)
return msg_list
changeFirst("now you are in love with me")
mow nou yre an iove lith we
I want to write a funtion UnchangeFirst()
which reverses this function, for example it should work like this:
UnchangeFirst("mow nou yre an iove lith we")
now you are in love with me
How can I reverse this function?
Here it is!
def changedAgain(message2):
msg_list = list(message2)
j = 0
first=msg_list[0]
for i in range(len(msg_list)):
if msg_list[i]==' ':
msg_list[j]=msg_list[i+1]
msg_list[i+1]=msg_list[j]
j=i+1
msg_list[j]=first
return(''.join(msg_list))
message2="mow nou yre an iove lith we"
print(changedAgain(message2))
>>> "now you are in love with me"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.