1

I am curious how this works to produce the provided output. The output is 4 which is the 5th character in the alphabet (which is expected) and subsequent alphabetical characters work all the way to the z character which also returns 25.

Example program

#include <iostream>
#include <string>

using namespace std;

int main()
{
  string str = "e";
  int numericalvalue = ((str[0]) - 'a');  //this line of code im trying to understand
  cout << numericalvalue;
  return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
simon
  • 854
  • 1
  • 9
  • 23
  • 8
    Are you familiar with the ASCII table? – AndyG Aug 29 '16 at 14:54
  • 1
    characters are internally just numbers (see ASCII table) all you do is subtract the numbers the characters represent. – Hayt Aug 29 '16 at 14:54
  • Yes but the decimal value of the character e is 101. Is this subtracting the value of a from it and then just returning it? – simon Aug 29 '16 at 14:57
  • 1
    There is no need to downvote a legitimate question. The user might be new to programming. – Pavan Yalamanchili Aug 29 '16 at 14:58
  • @simon: Yes. I suppose my next question is if you are familiar with integer promotion? – AndyG Aug 29 '16 at 14:58
  • 1
    Yes all it does is 101(e) - 97(a) which results in 4 – Hayt Aug 29 '16 at 14:59
  • @AndyG no not really sorry. – simon Aug 29 '16 at 15:08
  • @simon: That's okay. Basically what everyone has said is correct. The characters 'a' etc. are mapped from their ASCII table values. That is, there exists a direct mapping between `char` and `int`. When you subtract two characters, the result is an `int` and is formed by subtracting the corresponding entries in the ASCII table. That is we could say that `char` is 'promoted' to an `int` on subtraction. – AndyG Aug 29 '16 at 15:30
  • @AndyG thank you that clarifies a lot of the mystery behind this. In retrospect, is there a way to do this backwards? Convert int to a char output? – simon Aug 29 '16 at 15:33
  • @simon: Indeed there is: [Example](http://coliru.stacked-crooked.com/a/1647e1b7e5231342). You just need to be careful that you don't get values too large. – AndyG Aug 29 '16 at 15:57
  • "4 which is the 5th character the alphabet (which is expected)": Not really unless you only care about the English alphabet. Real world text is not limited to English nor is English text limited to alphabetic characters nor are English letter characters even limited to alphabetic characters. Algorithms of this sort are rarely useful, though you can make them more appropriate to the task if you define your own alphabet (say in an array, vector, string or map) with all of the relevant characters in the problem domain. I've never had a task where the problem domain was the English alphabet. – Tom Blodget Aug 29 '16 at 23:07

4 Answers4

3

It has to do with the ASCII table.

Its character in the set is assigned a unique integer. In your case, the literal 'a' has the value of 97 in the ASCII table and the character e has the value of 101. By subtracting 101 - 97 = 4 you get the final result.

More on the ASCII table can be found here

KostasRim
  • 2,053
  • 1
  • 16
  • 32
2

char is an integral type, so it's implicitly convertible (specifically, it undergoes integral promotion) to type int. Each character that you can represent with a basic char has a numeric value from 0-255 (or -128 to 127). So saying 'a' - '0' is actually valid since it's exactly like saying 97 - 48 with ints.

1

The character 'a' has the ASCII decimal value 97.

Letters after 'a' have a decimal value greater 97.

The substraction

str[0] - 'a'

uses the ASCII decimal values which produces the letters position in the alphabet.

robor
  • 2,969
  • 2
  • 31
  • 48
1
char e = 'e';
char a = 'a';
std::cout << "e = " << static_cast<int>(e) << std::endl;
std::cout << "a = " << static_cast<int>(a) << std::endl;

computers don't have such abstract things like "characters". They only work with 1's and 0's.. (numbers). The computer works with a number but knows, once it get's printed out on the screen, use the graphic that's in a .ttf file that represents that "character".

Stack Danny
  • 7,754
  • 2
  • 26
  • 55