给定用户输入的字符串以及从该位置开始的位置,我们需要按字母顺序将字符镜像到字符串的长度。在此操作中,我们将“ a”更改为“ z”,将“ b”更改为“ y”,“ c”更改为“ x”,“ d”更改为“ w”,依此类推,意味着第一个字符变为最后一个字符,依此类推上。
Inpu t: p = 3 Input string = python Output : pygslm
Step 1: Input the string and position from we need to mirror the characters. Step 2: Creating a string which is stored in alphabetical order. Step 3: Create an empty string. Step 4: Then traverse each character up to the position from where we need a mirror and up to this sting is unchanged. Step 5: From that position up to the length of the string, we reverse the alphabetical order. Step 6: Return the string.
# Python program to find mirror characters in string def mirror(str1, n): # Creating a string having reversed # alphabetical order alphaset = "zyxwvutsrqponmlkjihgfedcba" l = len(str1) # The string up to the point specified in the # question, the string remains unchanged and # from the point up to the length of the # string, we reverse the alphabetical order result = "" for i in range(0, n): result = result + str1[i]; for i in range(n, l): result = (result + alphaset[ord(str1[i]) - ord('a')]); return result; # Driver function str1 = input("Enter the string ::>") n = int(input("Enter the position ::>")) result = mirror(str1, n - 1) print("The Result ::>",result)
输出结果
Enter the string ::> python Enter the position ::> 3 The Result ::> pygslm