alphabet and its position

So far I've got the code to display
Enter a character: a
a is the 1 in the alphabet
but I was wondering how it'd be possible to output the actual word(first, second, third..)?
User a letter of the alphabet either upper or lowercase the code tells him which letter it is
For example:
'A’ ➔ "'A' is the first letter of the alphabet
'b’ ➔ "'b' is the second letter of the alphabet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  #include<iostream>
using namespace std;
int main ()
{
    char c;
    char num;
    cout << "Enter a character : ";
    cin >> c;
    if (c >= 'A' && c <= 'Z')
        num = c - 'A';
    else if (c >= 'a' && c <= 'z')
        num = c - 'a';
    cout<<c<<" is the " <<(int) num+1<<" in the alphabet"<<endl;
    return 0;
}
Something like this, perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cctype>
#include <string>

int main()
{
    const std::string upper_case_letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;

    const std::string positions[26] = { "first", "second", "third", "fourth", "fifth",
                                        "sixth", "seventh", "eighth", "nineth", "tenth",
                                        "eleventh", "twelth", /* .. etc up to twentysixth */ };

    char ch ;
    std::cout << "enter a character: " ;
    std::cin >> ch ;

    std::cout << '\'' << ch << "' is " ;
    const std::size_t pos = upper_case_letters.find( std::toupper(ch) ) ;
    if( pos == std::string::npos ) std::cout << "' not a letter in the alphabet\n" ;
    else std::cout << " the " << positions[pos] << " letter of the alphabet\n" ; ;
}
Thank you! this helped so much :)
Topic archived. No new replies allowed.