Program to print reciprocal of letters
Given a string S, we need to find reciprocal of it. The reciprocal of the letter is found by finding the difference between the position of the letter and first letter ‘A’. Then moving the same number of steps from letter ‘Z’. The character that we reach after above steps is reciprocal.
Reciprocal of Z is A and vice versa because if you reverse the position of the alphabet A will be in the position of Z.
Similarly, T is the reciprocal of G, J is the reciprocal of Q.
Examples :
Input : PRAKHAR
Output : KIZPSZI
Input : VARUN
Output : EZIFM
Just use a mathematical formula which gives the reciprocal of each character
Reciprocal(x) = ASCII(‘Z’) – ASCII(x) + ASCII(‘A’)
The ASCII value of Z and A will change according
to the uppercase and lowercase.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void reciprcalString(string word)
{
char ch;
for ( int i = 0; i < word.length(); i++) {
if ( isupper (word[i])) {
ch = 'Z' - word[i] + 'A' ;
cout << ch;
}
else if ( islower (word[i])) {
ch = 'z' - word[i] + 'a' ;
cout << ch;
}
else {
cout << word[i];
}
}
}
int main()
{
string s = "Geeks for Geeks" ;
cout << "The reciprocal of " << s
<< " is - " << endl;
reciprcalString(s);
return 0;
}
|
Java
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;
public class GFG {
static void Reciprcalstring(String word)
{
char ch;
for ( int i = 0 ; i < word.length(); i++) {
ch = word.charAt(i);
if (Character.isLetter(ch)) {
if (Character.isLowerCase(ch)) {
ch = ( char )( 122 - ( int )(ch) + 97 );
}
else if (Character.isUpperCase(ch)) {
ch = ( char )( 90 - ( int )(ch) + 65 );
}
}
System.out.print(ch);
}
}
public static void main(String[] args)
{
String s = "Geeks for Geeks" ;
System.out.print( "The reciprocal of " + s + " is - "
+ "\n" );
Reciprcalstring(s);
}
}
|
Python3
def isupper(ch):
if ch > = 'A' and ch < = 'Z' :
return True
return False
def islower(ch):
if ch > = 'a' and ch < = 'z' :
return True
return False
def reciprocalString(word):
ch = ''
for i in range ( len (word)):
if isupper(word[i]):
ch = chr ( ord ( 'Z' ) -
ord (word[i]) + ord ( 'A' ))
print (ch, end = "")
elif islower(word[i]):
ch = chr ( ord ( 'z' ) -
ord (word[i]) + ord ( 'a' ))
print (ch, end = "")
else :
print (word[i], end = "")
if __name__ = = "__main__" :
s = "Geeks for Geeks"
print ( "The reciprocal of" , s, "is - " )
reciprocalString(s)
|
C#
using System;
class GFG {
static void Reciprcalstring( string word)
{
char ch;
for ( int i = 0; i < word.Length; i++) {
ch = word[i];
if (Char.IsLetter(ch) && ch < 128) {
if ( char .IsLower(ch)) {
ch = ( char )(122 - ( int )(ch) + 97);
}
else if ( char .IsUpper(ch)) {
ch = ( char )(90 - ( int )(ch) + 65);
}
}
Console.Write(ch);
}
}
public static void Main()
{
string s = "Geeks for Geeks" ;
Console.Write( "The reciprocal of " + s +
" is - " + "\n" );
Reciprcalstring(s);
}
}
|
Javascript
<script>
function isLetter(ch){
if (ch>= 'a' && ch<= 'z' || ch>= 'A' && ch<= 'Z' )
return true ;
else
return
false ;
}
function isLowerCase(ch){
if (ch>= 'a' && ch<= 'z' )
return true ;
else
return
false ;
}
function isUpperCase(ch){
if ( ch>= 'A' && ch<= 'Z' )
return true ;
else
return
false ;
}
function Reciprcalstring( word) {
var ch;
for ( var i = 0; i < word.length; i++) {
ch = word.charAt(i);
if (isLetter(ch)) {
if (isLowerCase(ch)) {
ch =String.fromCharCode(122 -ch.charCodeAt(0) + 97);
}
else if (isUpperCase(ch)) {
ch =String.fromCharCode(90- ch.charCodeAt(0) + 65);
}
}
document.write(ch);
}
}
var s = "Geeks for Geeks" ;
document.write( "The reciprocal of " + s + " is - " + "<br\>" );
Reciprcalstring(s);
</script>
|
PHP
<?php
function check_lowercase_string( $string )
{
return ( $string === strtolower ( $string ));
}
function check_uppercase_string( $string )
{
return ( $string === strtoupper ( $string ));
}
function Reciprcalstring( $word )
{
$ch ;
for ( $i = 0; $i < strlen ( $word ); $i ++)
{
$ch = $word [ $i ];
if ( $ch == ' ' )
echo ( $ch );
else if (check_lowercase_string( $ch ))
{
$ch = chr (122 -
ord( $ch ) + 97);
}
else if (check_uppercase_string( $ch ))
{
$ch = chr (90 -
ord( $ch ) + 65);
}
echo ( $ch );
}
}
$s = "Geeks for Geeks" ;
echo ( "The reciprocal of " .
$s . " is - " . "\n" );
Reciprcalstring( $s );
?>
|
Output
The reciprocal of Geeks for Geeks is -
Tvvph uli Tvvph
Time Complexity: O(n), where n is the length of the string.
Auxiliary Space: O(1)
Last Updated :
29 Feb, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...