14
Here’s what I need to do: string
and go through it and pick up each letter found and add with its corresponding value, type: a = 1, s = 19 and etc.
Well, I made a enum
with all the values of string
, starting with a = 1 to z = 26 (includes K,W and Y). I’m having difficulty picking up the letter in for
and accumulate its value in relation to enum
.
public enum triaguloLetra
{
a = 1,
b = 2,
c = 3,
d = 4,
e = 5,
f = 6,
g = 7,
h = 8,
i = 9,
j = 10,
k = 11,
l = 12,
m = 13,
n = 14,
o = 15,
p = 16,
q = 17,
r = 18,
s = 19,
t = 20,
u = 21,
v = 22,
w = 23,
x = 24,
y = 25,
z = 26
}
string teste = "Stackoverflow";
for (int i = 0; i <= teste.Length - 1; i++)
{
//Como eu digo que teste[i] = ao enum.s?? e assim por diante
}
I think it would be better to use one
Dictionary<char, int>
instead ofDictionary<string, int>
– dcastro
@dcastro Yes, I would avoid the call to
ToString()
.– ramaral
@ramaral, excuse my ignorance, but I did not understand 'a' + 1 in the lambda above. It worked without Dictionary<>, but I did not understand why 'a' + 1.
– pnet
char
is no more than an instance of the structure System.Char which is used to represent characters Unicode and whose value is an integer. The value of 'a' is 97, 'b' is 98 and so on. With you want the sequence to start at 1 we have to subtract from the value of each letter the value of 'a' plus 1. Example for the letter 'b' => 98 - 'a'(97) + 1 = 2.– ramaral
@I understood, although it was a bit strange. So "c" would be: => 99 - 'a'(97) + 1 = 3. That’s it, right?
– pnet
Exactly. Each character has one code associated, it is this code that
char
represents.– ramaral