Python Program to find mirror characters in a string


This article teaches you how to write a python program to find mirror characters in a string.

Let us first understand what mirror characters in a string are. Two identical alphabetically positioned characters, one from the front and the other from the back, are known as mirror characters. For example, the alphabet a's mirror character is "z," "b's" is "y," and so on.

Input-Output Scenario

Following is an input and its output example of mirror characters in a string −

Input: p = 3
Input string = Coding
Output: Cowrmt

Here, the alphabets “d”, “i”, “n”, “g” in the string “coding” lies in the position 4, 9, 14 and 7 respectively. Hence, its mirror characters “w”, “r”, “m”, “t” also lies in the position 4, 9 14 and 7 respectively from the reverse order of the alphabets. Since the position “p” is equals to 3, so it started from the third position of the string “Coding”.

Using loops

One way to resolve this is using simple loops following is an algorithm to do so −

Algorithm

  • Enter the string and the position where the characters need to be mirrored.

  • Creating an alphabetically-sorted string to be stored.

  • Create a string that is empty.

  • Then move along each character up to the position where we require a mirror, keeping everything up until that position unchanged.

  • We reverse the alphabetical order starting there and continuing all the way to the end of the string.

  • The string should be returned.

Example

Following is an example to find the mirror characters in a string using loop −

def mirror_characters(string, x): # Create a string with reversed alphabetical order alphabet = "zyxwvutsrqponmlkjihgfedcba" z = len(string) # Reversing the alphabetical order of the string from the given position leaving the rest unchanged result = "" for i in range(0, x): result = result + string[i]; for i in range(x, z): result = (result + alphabet[ord(string[i]) - ord('a')]); return result; # The driver code string = input("Enter the string name ::>") x = int(input("Enter the position to start from ::>")) result = mirror_characters(string, x - 1) print("The Result is ::>",result)

Output

Following is an output of the above code −

Enter the string name ::>TutorialsPoint
Enter the position to start from ::>4
The Result is ::> Tutlirzohqlrmg

Using dictionary

Using the idea of a dictionary, we will carry out this work in Python. We will create an alphabetical dictionary of characters and map it to characters in the reverse manner. Then, using a dictionary, we will iterate through the characters starting at position k of the string and convert them to their mirror values.

Algorithm

For a better understanding of the strategy, follow the algorithm −

  • Create a function that mirrors the characters in the place after the nth position.

  • Set variables in the proper reverse and alphabetical order.

  • Build a dictionary.

  • The string has to be sliced into two parts at n.

  • Mirror each character as you traverse to the second part of the string.

  • The first string and the mirrored string should be joined.

  • As a result, print after joining strings.

  • Create a string and n value.

  • In the function, pass the string and n.

Example

For an understanding of how the above strategy is being implemented, look at the programme below. We have built a dictionary using the dict() and zip() methods, with the alphabetical characters serving as the key and the reversed characters serving as the value.

def MirrorCharacter(str,n): # creating the dictionary alphabetical_order = 'abcdefghijklmnopqrstuvwxyz' reverse_alphabetical_order = 'zyxwvutsrqponmlkjihgfedcba' dictionaryCharacters = dict(zip(alphabetical_order,reverse_alphabetical_order)) # dividing the string at n prefix = str[0:n-1] suff = str[n-1:] mirror = '' # changing the suffix into mirror characters for i in range( len(suff)): mirror = mirror + dictionaryCharacters[suff[i]] # joining the prefix and the mirrored part return (prefix+mirror) string = 'Tutorialspoint' n = 6 print("The mirrored value is: ",MirrorCharacter(string,n))

Output

Following is an output of the above code −

The mirrored value is:  Tutorr

Example

Following is an example to find the mirror characters in a string using dict −

dictionaryCharacters = dict( zip( 'abcdefghijklmnopqrstuvwxyz', 'zyxwvutsrqponmlkjihgfedcba' ) ) def mirror(str, x): str = list(str) for i in range(x-1, len(str)): print(i, str[i], dictionaryCharacters[str[i]]) str[i] = dictionaryCharacters[str[i]] return ''.join(str) assert mirror('Tutorialspoint', 6), 'stipulation' assert mirror('Incomprehensibility', 8), 'prominence'

Output

Following is an output of the above code −

5 i r
6 a z
7 l o
8 s h
9 p k
10 o l
11 i r
12 n m
13 t g
7 e v
8 h s
9 e v
10 n m
11 s h
12 i r
13 b y
14 i r
15 l o
16 i r
17 t g
18 y b

Updated on: 23-Nov-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements