The number place of a letter in the alphabet

closed account (ENh0ko23)
For example: The letter B is the 2nd letter in the alphabet and the letter N is the 14th letter in the alphabet. How would i even make that into a c++???
Try subtraction:

1
2
3
4
5
  int pos;
  char c;
  std::cin >> c;
  pos = c - 'A';  // Assumes c is upper case
  //  pos is 0 based.  i.e. A is 0  Add 1 if you want 1 based 


Hi Uhyiluh

same way as in my previous post for you, ascii code for this is i think best choice,
all letters and other charactes have unique number. for you then is just to build a code that will count this.

closed account (ENh0ko23)
TBH i am confused by both of these posts. The subtraction thing didnt work the way i want and i dont get ASCII
just write out your own character set:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <vector>
#include <algorithm>
#include <cctype>

static const std::vector<char> alphabet
    {'A','B','C','D','E','F','G','H','I','J','K','L','M','N', 'O', 'P','Q','R','S','T','U','V','W','X','Y','Z'};

int main()
{
    std::cout << "Enter the letter \n";
    char input{};
    std::cin >> input;
    input = std::toupper(input);
    auto itr = std::find(alphabet.begin(), alphabet.end(), input);
    if (itr != alphabet.end())
    {
        std::cout << "letter entered occupies position " << itr - alphabet.begin() + 1 << " in the alphabet \n";
    }
    else
    {
        std::cout << "charachter entered is not present in the alphabet \n";
    }
}
Topic archived. No new replies allowed.