 How to replace a alphabet with it's corresponding one? | Sololearn: Learn to code for FREE!

+2

# How to replace a alphabet with it's corresponding one?

This could be done by creating two char arrays that consist of alphabet and backward alphabet respectively. But i am searching for pre defined function to do this as creating two char arrays of 26 elements each would be time consuming plus inefficient.

+1

You can just do something like cin >> ch; ch = toupper(ch);//#<cctype> cout <<(char) ('Z'-(ch-'A'))<< endl;

+2

Wdym by its corresponding one ? You state having one backward so like {A,B,C } {C,B,A } ??

+1

+1

Whats an example of input and out you expecting ?

+1

Raul Ramirez Input : A output: Z Is there any pre defined function out there to do so?

+1

All chars have an associated decimal number (ascii table ) (ch -‘A’) -> ch - 65 Basically how far from 65 am I => disp (‘Z‘- disp ) -> 90 - disp From 90 take away the disp (char) cast back to a character

+1

Manav Roy You could create 1 alphabet consisting of 26 letters with string indexes ranging from 0 to 25 IE: A = 0, Z = 25 In order to get the reverse letter, you can use 25 - index of letter IE: A = 0. 25 - 0 = 25 = Z D = 3. 25 - 3 = 22 = V Y = 24. 25 - 24 = 1 = B Of course, you could use different alphabets

+1

char self = 'A'; char complement = 'Z'-self%'A'; cout<< complement << endl;

0

Raul Ramirez Hello, Though it's working alright, But i just didn't get it's syntax 🤔

0

Vinit Sonawane This one is also good,Thanks :)