If it’s a letter, prints out where it is in the alphabet

Hi so im trying to write a program where one inputs a character and the output tells you where the letter stands in the alphabet and also the write suffix for the number. For ex: c=3rd letter of the alphabet
I need it to work for both upper and lower case characters.
I am doing this way but it seems very long and tedious, is there another way where i can just pull out the number of the character and also the suffix?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

#include <iostream> 

int main() {
char faveChar;

//prompt user for input
  cout << "Input your favorite character:\n";
    cin >> faveChar;
//If it's a letter, print out where it is in the alphabet & proper suffix
if (faveChar == 'a' || faveChar == 'A')
        cout << faveChar << " is the 1st letter of the alphabet.\n";
    else if (faveChar == 'b' || faveChar =='B')
        cout << faveChar << " is the 2nd letter of the alphabet.\n";
    else if (faveChar == 'c' || faveChar =='C')
        cout << faveChar << " is the 3rd letter of the alphabet.\n";
    else if (faveChar == 'd' || faveChar =='D')
        cout << faveChar << " is the 4th letter of the alphabet.\n";

}
This might help:
1
2
3
4
5
6
7
8
#include <iostream>
#include <cctype>
int main ()
{
  char c='c';
  std::cout<<toupper(c)-'A'+1<<std::endl;
  return 0;
}


toupper will convert your character to upper case. If you subtract 'A' will get you the distance between your given character and the first character in the alphabet. so if you input 'a', the answer of toupper(c)-'A' will be 0. So you should add 1
Topic archived. No new replies allowed.