PROBLEM LINK:
Author: rahul_ojha_07
Tester: horsbug98
DIFFICULTY:
EASY
PREREQUISITES:
Modulo Operation, Basic Mathematics
PROBLEM:
Given a string containing Characters and numbers replace each character by the specified character in the Question.
EXPLANATION:
We can solve this question very easily by the use of the ASCII codes of the characters. without the need of specifying which characters should replace which character (using if-else or switch - case).
According to the question we have to replace characters:
- a'-'m' with 'n'-'z'
- 'n'-'z' with 'a'-'m'
- 'A'-'M' with 'N'-'Z'
- 'N'-'Z' with 'A'-'M'
- '0'-'4' with '5'-'9'
- '5'-'9' with '0'-'4'
The ASCII code of character βaβ is 97 and βnβ is 110 we can get to βnβ from βaβ with the addition of 13 to ASCII code of βaβ, similarly by adding 13 to the ASCII code of βbβ we get the character βoβ and similarly by adding 13 to the ASCII code of a char we can get the desired so on till the character βmβ(109). so for the first condition, we can say if the Ascii code of a character is greater than or equal to 97 and less than or equal to 109 we can get the desired character by adding 13 to it.
Similarly also for Uppercase characters from βAβ(65) to βMβ(77) we can add 13 to the ASCCI code and get Characters from βNβ(78) to βZβ(90).
if (AsciiCode >= 97 and AsciiCode <= 109) or (AsciiCode >= 65 and AsciiCode <= 77):
AsciiCode + = 13
Now, we can get βaβ from βnβ by subtracting 13 from the ASCII code of βnβ. We can use technique this to get the desired character from βnβ(110) - βzβ(122).
Similarly also for Uppercase characters from βNβ(78) to βZβ(90) we can subtract 13 from the ASCCI code and get Characters from βAβ(65) to βMβ(77).
if (AsciiCode >= 110 and AsciiCode <= 122) or (AsciiCode >= 78 and AsciiCode <= 90):
AsciiCode - = 13
By following this method we can also get the digit replacement, for example, the ASCII code for β0β is 48 and ASCII code for β5β is 53 so in this case, we can add 5 to the digit and get the desired digit i.e.
if (AsciiCode >= 48 and AsciiCode <= 52):
AsciiCode + = 5
and for getting β0β-β4β from β5β-β9β we can subtract 5 from the ascci code i.e.
if (AsciiCode >= 53 and AsciiCode <= 57):
AsciiCode - = 5
Using this simple method for every character we can get the desired string.
Authorβs solution can be found here.