Reverse Alphabet no arrays

I was wondering how to write code that would take user input, such as 'A', and mirror that. A would be Z, B would be Y and so forth.I cannot use arrays or strings. I am reading character by character. I cannot use arrays and need to stick with loop and if statement basics essentially. thank you for any ideas or tips!
1
2
3
4
5
6
7
8
 void reverseUppercase(char & alphabet)
{
  /*can I use a while loop? am i doing this 
wrong with if? here is the only thing I can 
think of, but if it isn't Z, then it doesn't 
really work right?*/
   26 - alphabet + 1;
}


do I assign 65 to 90 instead and work like that (even though i dont know how i could do that)?
maybe i could assign alphabet to a rang of 1-26 so i can work from that rang?
Last edited on
alphabet = 'Z' - alphabet + 'A';
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;

int main() {
	
	for (int alphabet = 65 ; alphabet < 91 ; alphabet ++)
	{
		cout << char(alphabet) << " becomes " << char('Z' - alphabet + 'A') << '\n';
	}
	return 0;
}


Don't forget to worry about what happens if the input isn't between 'A' and 'Z'.
Last edited on
1
2
3
4
5
6
7
void reverseAlpha(char& ch)
{
    if (ch >= 'A' && ch <= 'Z')
        ch = 25 - (ch - 'A') + 'A';
    else if (ch >= 'a' && ch <= 'z')
        ch = 25 - (ch - 'a') + 'a';
}

thank you both! it makes so much more sense than everything I had tried. Thank you so much. Don't worry, I have a function for validation :)
Topic archived. No new replies allowed.