Mirror characters of a string
Given a string and a number N, we need to mirror the characters from N-th position up to the length of the string in the alphabetical order. In mirror operation, we change ‘a’ to ‘z’, ‘b’ to ‘y’, and so on.
Examples:
Input : N = 3
paradox
Output : paizwlc
We mirror characters from position 3 to end.
Input : N = 6
pneumonia
Output : pnefnlmrz
Below are different characters and their mirrors.
Mirroring the alphabetical order means that a corresponds to z, b corresponds to y. Which means that first character becomes the last and so on. Now, to achieve this we maintain a string(or a character array) which contains the English alphabets in lower case. Now from the pivot point up to the length, we can look up the reverse alphabetical order of a character by using its ASCII value as an index. Using the above technique, we transform the given string in the required one.
Implementation:
C++
#include <iostream>
#include <string>
using namespace std;
string compute(string str, int n)
{
string reverseAlphabet = "zyxwvutsrqponmlkjihgfedcba" ;
int l = str.length();
for ( int i = n; i < l; i++)
str[i] = reverseAlphabet[str[i] - 'a' ];
return str;
}
int main()
{
string str = "pneumonia" ;
int n = 4;
string answer = compute(str, n - 1);
cout << answer;
return 0;
}
|
Java
import java.io.*;
class GeeksforGeeks {
static String compute(String str, int n)
{
String reverseAlphabet = "zyxwvutsrqponmlkjihgfedcba" ;
int l = str.length();
String answer = "" ;
for ( int i = 0 ; i < n; i++)
answer = answer + str.charAt(i);
for ( int i = n; i < l; i++)
answer = answer + reverseAlphabet.charAt(str.charAt(i) - 'a' );
return answer;
}
public static void main(String args[])
{
String str = "pneumonia" ;
int n = 4 ;
System.out.print(compute(str, n - 1 ));
}
}
|
Python3
def compute(st, n):
reverseAlphabet = "zyxwvutsrqponmlkjihgfedcba"
l = len (st)
answer = ""
for i in range ( 0 , n):
answer = answer + st[i];
for i in range (n, l):
answer = (answer +
reverseAlphabet[ ord (st[i]) - ord ( 'a' )]);
return answer;
st = "pneumonia"
n = 4
answer = compute(st, n - 1 )
print (answer)
|
C#
using System;
class GFG {
static String compute( string str, int n)
{
string reverseAlphabet =
"zyxwvutsrqponmlkjihgfedcba" ;
int l = str.Length;
String answer = "" ;
for ( int i = 0; i < n; i++)
answer = answer + str[i];
for ( int i = n; i < l; i++)
answer = answer +
reverseAlphabet[str[i]- 'a' ];
return answer;
}
public static void Main()
{
string str = "pneumonia" ;
int n = 4;
Console.Write(compute(str, n - 1));
}
}
|
php
<?php
function compute( $str , $n )
{
$reverseAlphabet =
"zyxwvutsrqponmlkjihgfedcba" ;
$l = strlen ( $str );
$answer = "" ;
for ( $i = 0; $i < $n ; $i ++)
$answer = $answer . $str [ $i ];
for ( $i = $n ; $i < $l ; $i ++){
$answer = $answer . $reverseAlphabet [
ord( $str [ $i ])- ord( 'a' )];
}
return $answer ;
}
$str = "pneumonia" ;
$n = 4;
$answer = compute( $str , $n - 1);
echo $answer ;
?>
|
Javascript
<script>
function compute(str, n)
{
let reverseAlphabet =
"zyxwvutsrqponmlkjihgfedcba" ;
let l = str.length;
let answer = "" ;
for (let i = 0; i < n; i++)
answer = answer + str[i];
for (let i = n; i < l; i++)
answer = answer + reverseAlphabet[str[i].charCodeAt()- 'a' .charCodeAt()];
return answer;
}
let str = "pneumonia" ;
let n = 4;
document.write(compute(str, n - 1));
</script>
|
Output:
pnefnlmrz
Time Complexity: O(n), where n represents the size of the given string.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Last Updated :
25 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...