Computer Science Canada Assigning Letter Values |
Author: | Anonymous [ Tue Aug 22, 2006 5:52 pm ] | ||
Post subject: | Assigning Letter Values | ||
How would I automate this code in a for loop preferably?
|
Author: | TokenHerbz [ Tue Aug 22, 2006 6:14 pm ] | ||
Post subject: | |||
You should really learn about "ord" and "chr", there amazing
|
Author: | pj_ladd12 [ Tue Aug 22, 2006 8:27 pm ] |
Post subject: | |
why did you 64+i? why 64? |
Author: | TokenHerbz [ Tue Aug 22, 2006 8:42 pm ] |
Post subject: | |
to get the capital alphabets, go ahead and take out "64" and see what happends. |
Author: | Cervantes [ Tue Aug 22, 2006 8:45 pm ] |
Post subject: | |
"A" is given the ASCII value 65. "B" is 66, and so on. So since we're starting with an index of 1, we need to add 64 to our index to get us to the range of the capital letters. To get lower case letters like the author wanted, add a higher value. "a" is ASCII value 97. |
Author: | Clayton [ Wed Aug 23, 2006 1:55 am ] | ||
Post subject: | |||
another way to do it (not as good, but works fine) is to have a string variable with all of the letters of the alphabet (lower and upper case) and simply having a for loop giving the value of a letter to an element in your array, for example:
|
Author: | Cervantes [ Wed Aug 23, 2006 7:36 am ] |
Post subject: | |
Since a string is basically an array of characters, you've got two arrays of characters there. Not so efficient. You could probably work with your string ("abcd...XYZ") just as easily as you work with the array that was originally posted. |
Author: | Wolf_Destiny [ Wed Aug 23, 2006 8:05 am ] | ||
Post subject: | |||
Here you go:
Four lines of quick for loop goodness. To make capitals instead, all you have to do it change 'a' to 'A' and 'z' to 'Z' It also works with numbers... or any characters really Hope that helps |
Author: | TokenHerbz [ Wed Aug 23, 2006 2:38 pm ] |
Post subject: | |
my way is best, as it is far easier to get any characters needed, and just as easy to change them. |
Author: | Cervantes [ Wed Aug 23, 2006 3:10 pm ] |
Post subject: | |
They're basically the exact same thing. Let's not squabble over that. The question has been answered. No more flogging a dead horse. |