Sorry if the title is misleading or is confusing, but here is my dilemma. I am inputting a string, and want to assign a value to each capitalized letter in the alphabet (A=1, ..
It would look something like this (in C programming language) which you can easily modify for other programming languages:
#include
#include
#include
int main() {
int i;
char word[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
unsigned int sum = 0;
unsigned int charVal;
for (i=0; i < strlen(word); ++i) {
charVal = word[i] - 'A' + 1;
printf("Value of %c is %d\n", word[i], charVal);
sum += charVal;
}
printf("Sum of %s = %d\n", word, sum);
return(0);
}
The trick is to take the character value, subtract the baseline 'A' value and add 1 to arrive at your calculation range:
charVal = word[i] - 'A' + 1;