0

I have a problem, I need to relate a value to a letter. The values come in an array. For example:

Value vector -> [1,2,3,1,2,5,1,...,8]

I need an idea to make the first number of that vector be linked to the letter "a", the 2nd number to the letter "b", etc until the last number to the letter "z".

So, a -> 2, value of "a" is 2.

The purpose of this is if I have a word like "air" I want the value of air to be the value of "a" + the value of "i" + the value of "r".

Edward A
  • 2,291
  • 2
  • 18
  • 31
Blackfing
  • 5
  • 5
  • What is this value vector? Why is a not linked to 1 – coder hacker May 12 '14 at 15:56
  • I have a file, that's something like this: a 2 b 1 c 5 etc.. I'm thinking of storing those numbers in an array, so I can letter assign each letter to the value. If you have a better solution I'm all ears. – Blackfing May 12 '14 at 15:57
  • What is the type of `Value`? – metacubed May 12 '14 at 15:58
  • Than why can't you add the values if you know which letter belongs to which number? – coder hacker May 12 '14 at 15:58
  • value is an int vec[number of letters in the alphabet]; – Blackfing May 12 '14 at 15:59
  • I know which letter belong to whitch number, but the program doesn't, and if I access the file too many times it'll consume a lot of time. This program is supposed to run a dictionary with 411.000 words, the time that would consume would be absurd. – Blackfing May 12 '14 at 16:01

1 Answers1

1

Your problem seems to be that you can't get the array index from a letter. The twenty-six lowercase Latin letters are a contiguous block in ASCII, and you can get the ASCII code of a character with the single-quote notation, hence:

int ix = c - 'a'

Note that this will give you invalid indices for your array if your character c is not a letter. If your alphabet is not the plain Latin alphabet, you could write a function to assign a numerical index to your letter. For example, if I wanted to write an index function for the German alphabet, I would do something like this:

int index_de(int c) {
    if (c == 'ä') return 26;
    if (c == 'ö') return 27;
    if (c == 'ü') return 28;
    if (c == 'ß') return 29;
    if (c < 'a' || c > 'z') return -1;
    return c - 'a';
}

(Because the accented letters are outside the pure 7-bit ASCII range, this will introduce issues of source-code and input encoding. Be warned.)

You can then use this function to assign scores to letter codes when you (a) read the file and then (b) scan the words you want to score.

M Oehm
  • 28,726
  • 3
  • 31
  • 42
  • Thanks, that's exactly what I was missing. I wonder if I can define your variable "c" to be 97, so a-c would be vector[0], b-c would be vector[1], etc. – Blackfing May 12 '14 at 16:30
  • The character `'a'` is an integer constant with the value of 97. You could have (a possibly global) variable or a `#define`d) macro with the value of 97, but I recommend to use this notation - it's immediately clear that you are doing arithmetics on characters. (The same goes for `x - '0'`, where one can see at a glance that a converson from a string to a numeric value is taking place.) – M Oehm May 12 '14 at 18:04