Hi there,
can anyone help me with this scenario.?
If I give a string as input, it should give as output, the sum total of its positional value.
eg;
a=1
b=2
.
.
.
z=26
now if I give the string “good” as input the output should be 41. (g=7,o=15,o=15,d=4 and 7+15+15+4=41).
thanks in advance…
static void wordToNumber(Args _args)
{
int length;
int i = 1;
int wordIntoNum;
Map letterMap = new Map(Types::String, Types::Integer);
#define.value(‘DAD’)
;
letterMap.insert(‘A’, 1);
letterMap.insert(‘B’, 2);
letterMap.insert(‘C’, 3);
letterMap.insert(‘D’, 4);
length = strlen(#value);
for (i = 1; i <= length; i++)
{
wordIntoNum += letterMap.lookup(substr(#value, i, 1));
}
info(int2str(wordIntoNum));
}
A quick and dirty solution:
str input = "good";
int i, total;
for (i = 1; i <= strLen(input); i++)
{
total += char2num(input, i) - 96;
}
I think we should also consider whether the letters are capital or not when using char2num in this situation as they have different ASCII values
http://en.wikipedia.org/wiki/ASCII
Of course, if I get more detailed requirements (such as capital letters, which haven’t been mentioned in the question), I would look for some less quick and dirty solution - but I think it’s better not to overcomplicate examples from the beginning with potential requirements. [:)]
thank you so much for the reply friend…!