Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Interview Problems on String
  • Practice String
  • MCQs on String
  • Tutorial on String
  • String Operations
  • Sort String
  • Substring & Subsequence
  • Iterate String
  • Reverse String
  • Rotate String
  • String Concatenation
  • Compare Strings
  • KMP Algorithm
  • Boyer-Moore Algorithm
  • Rabin-Karp Algorithm
  • Z Algorithm
  • String Guide for CP
Open In App
Next Article:
Program to print reciprocal of letters
Next article icon

Program to print reciprocal of letters

Last Updated : 29 Feb, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report
Try it on GfG Practice
redirect icon

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

Recommended Practice
Program to print reciprocal of letters - copy
Try It!

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++
// CPP program to find Reciprocal string #include <bits/stdc++.h> using namespace std;  // To print reciprocal string void reciprcalString(string word) {     char ch;     for (int i = 0; i < word.length(); i++) {         // converting uppercase character         // To reciprocal character         // display the character         if (isupper(word[i])) {             ch = 'Z' - word[i] + 'A';             cout << ch;         }          // converting lowercase character         // To reciprocal character         // display the character         else if (islower(word[i])) {             ch = 'z' - word[i] + 'a';             cout << ch;         }          else {             cout << word[i];         }     } }  // Driver function int main() {     string s = "Geeks for Geeks";     cout << "The reciprocal of " << s          << " is - " << endl;     reciprcalString(s);     return 0; } 
Java
// JAVA program to illustrate... // Reciprocal Letters import java.io.*; import java.math.*; import java.text.*; import java.util.*; import java.util.regex.*;  public class GFG {      // function to print     // the reciprocal     static void Reciprcalstring(String word)     {         char ch;         for (int i = 0; i < word.length(); i++) {             ch = word.charAt(i);              // Checking if the character             // is a letter or not             if (Character.isLetter(ch)) {                  // converting lowercase character                 // To reciprocal character                 if (Character.isLowerCase(ch)) {                     ch = (char)(122 - (int)(ch) + 97);                 }                 // converting uppercase character                 // To reciprocal character                 else if (Character.isUpperCase(ch)) {                     ch = (char)(90 - (int)(ch) + 65);                 }             }              // display each character             System.out.print(ch);         }     }      // Driver function     public static void main(String[] args)     {         // static input         String s = "Geeks for Geeks";         System.out.print("The reciprocal of " + s + " is - "                          + "\n");          // calling the function         Reciprcalstring(s);     } } 
Python3
# Python3 program to find Reciprocal string  # to check for UPPERCASE def isupper(ch):     if ch >= 'A' and ch <= 'Z':         return True     return False  # to check for LOWERCASE def islower(ch):     if ch >= 'a' and ch <= 'z':         return True     return False  # To print reciprocal string def reciprocalString(word):     ch = ''     for i in range(len(word)):          # converting uppercase character         # To reciprocal character         # display the character         if isupper(word[i]):             ch = chr(ord('Z') -                       ord(word[i]) + ord('A'))             print(ch, end = "")          # converting lowercase character         # To reciprocal character         # display the character         elif islower(word[i]):             ch = chr(ord('z') -                       ord(word[i]) + ord('a'))             print(ch, end = "")         else:             print(word[i], end = "")  # Driver Code if __name__ == "__main__":     s = "Geeks for Geeks"     print("The reciprocal of", s, "is - ")     reciprocalString(s)  # This code is contributed by # sanjeev2552 
C#
// C# program to find Reciprocal string using System;  class GFG {      // function to print the reciprocal     static void Reciprcalstring(string word)     {         char ch;         for (int i = 0; i < word.Length; i++) {             ch = word[i];              // Checking if the character             // is a letter or not             if (Char.IsLetter(ch) && ch < 128) {                  // converting lowercase character                 // To reciprocal character                 if (char.IsLower(ch)) {                     ch = (char)(122 - (int)(ch) + 97);                 }                                  // converting uppercase character                 // To reciprocal character                 else if (char.IsUpper(ch)) {                     ch = (char)(90 - (int)(ch) + 65);                 }             }              // display each character             Console.Write(ch);         }     }      // Driver function     public static void Main()     {         string s = "Geeks for Geeks";         Console.Write("The reciprocal of " + s +                                " is - " + "\n");          // calling the function         Reciprcalstring(s);     } }  // This code is contributed by Sam007 
JavaScript
<script> // javascript program to illustrate... // Reciprocal Letters 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 to print     // the reciprocal     function Reciprcalstring( word) {         var  ch;         for (var i = 0; i < word.length; i++) {             ch = word.charAt(i);              // Checking if the character             // is a letter or not             if (isLetter(ch)) {                  // converting lowercase character                 // To reciprocal character                 if (isLowerCase(ch)) {                     ch =String.fromCharCode(122 -ch.charCodeAt(0) + 97);                 }                 // converting uppercase character                 // To reciprocal character                 else if (isUpperCase(ch)) {                     ch =String.fromCharCode(90- ch.charCodeAt(0) + 65);                                  }             }              // display each character             document.write(ch);         }     }      // Driver function              //  input         var s = "Geeks for Geeks";         document.write("The reciprocal of " + s + " is - " + "<br\>");          // calling the function         Reciprcalstring(s);  // This code is contributed by umadevi9616  </script> 
PHP
<?php // PHP program to find // Reciprocal string  function check_lowercase_string($string)  {     return ($string === strtolower($string)); }  function check_uppercase_string($string)  {     return ($string === strtoupper($string)); }  // function to print // the reciprocal function Reciprcalstring($word) {     $ch;     for ($i = 0; $i < strlen($word); $i++)      {         $ch = $word[$i];                  // Check if space,         // then print it         if($ch == ' ')             echo ($ch);                      // converting lowercase character         // To reciprocal character         else if (check_lowercase_string($ch))         {             $ch = chr(122 -                        ord($ch) + 97);         }                  // converting uppercase character         // To reciprocal character         else if (check_uppercase_string($ch))          {             $ch = chr(90 -                        ord($ch) + 65);         }                  // display each          // character         echo ($ch);     } }  // Driver Code $s = "Geeks for Geeks"; echo ("The reciprocal of ".         $s. " is - ". "\n");  // calling the function Reciprcalstring($s);  // This code is contributed by  // Manish Shaw(manishshaw1) ?> 

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)


Next Article
Program to print reciprocal of letters

M

Manish_100
Improve
Article Tags :
  • Strings
  • DSA
  • ASCII
Practice Tags :
  • Strings

Similar Reads

    Program to print ASCII Value of a character
    Given a character, we need to print its ASCII value in C/C++/Java/Python. Examples : Input : a Output : 97 Input : DOutput : 68 Here are few methods in different programming languages to print ASCII value of a given character :  Python code using ord function : ord() : It converts the given string o
    4 min read
    Program to print Reverse Floyd's triangle
    Floyd’s triangle is a triangle with first natural numbers. Task is to print reverse of Floyd’s triangle.Examples: Input : 4 Output : 10 9 8 7 6 5 4 3 2 1 Input : 5 Output : 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 C++ // CPP program to print reverse of // floyd's triangle #include <bits/stdc++.h> u
    3 min read
    Program to Print Alphabets From A to Z Using Loop
    Our task is to print the alphabets from A to Z using loops. There are various methods to print alphabets from (A to Z) or (a to z).Using ASCII valuesUsing character variables.In this article we will mainly focus on the following programs and their logic:Using for loopUsing the while loopUsing a do-w
    15 min read
    Program to convert Number in characters
    Given an Integer N. The task is to convert the number in characters. Examples: Input: N = 74254 Output: Seven four two five four Input: N = 23 Output: Two three An efficient approach: Reverse the number.Iterate through the reversed number from right to left.Extract the last digit by using modulus, t
    6 min read
    Write a program to reverse digits of a number
    Given an Integer n, find the reverse of its digits.Examples: Input: n = 122Output: 221Explanation: By reversing the digits of number, number will change into 221.Input: n = 200Output: 2Explanation: By reversing the digits of number, number will change into 2.Input: n = 12345 Output: 54321Explanation
    8 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences