Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Assigning Letter Values
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Guest




PostPosted: Tue Aug 22, 2006 5:52 pm   Post subject: Assigning Letter Values

How would I automate this code in a for loop preferably?
code:

letter (1) := 'a'
letter (2) := 'b'
letter (3) := 'c'
letter (4) := 'd'
letter (5) := 'e'
letter (6) := 'f'
letter (7) := 'g'
letter (8) := 'h'
letter (9) := 'i'
letter (10) := 'j'
letter (11) := 'k'
letter (12) := 'l'
letter (13) := 'm'
letter (14) := 'n'
letter (15) := 'o'
letter (16) := 'p'
letter (17) := 'q'
letter (18) := 'r'
letter (19) := 's'
letter (20) := 't'
letter (21) := 'u'
letter (22) := 'v'
letter (23) := 'w'
letter (24) := 'x'
letter (25) := 'y'
letter (26) := 'z'
Sponsor
Sponsor
Sponsor
sponsor
TokenHerbz




PostPosted: Tue Aug 22, 2006 6:14 pm   Post subject: (No subject)

You should really learn about "ord" and "chr", there amazing Smile

code:

var letter: array 1 .. 26 of string

for i: 1 .. upper (letter)
    letter(i) := chr(64+i)
    put letter(i) ..
end for
pj_ladd12




PostPosted: Tue Aug 22, 2006 8:27 pm   Post subject: (No subject)

why did you 64+i? why 64?
TokenHerbz




PostPosted: Tue Aug 22, 2006 8:42 pm   Post subject: (No subject)

to get the capital alphabets, go ahead and take out "64" and see what happends.
Cervantes




PostPosted: Tue Aug 22, 2006 8:45 pm   Post subject: (No 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.
Clayton




PostPosted: Wed Aug 23, 2006 1:55 am   Post subject: (No 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:

Turing:

var alphabet : string := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
var my_letters : array 1 .. length(alphabet) of string

for x: 1..upper(my_letters)
    my_letters(x):=alphabet(x)
end for
Cervantes




PostPosted: Wed Aug 23, 2006 7:36 am   Post subject: (No 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.
Wolf_Destiny




PostPosted: Wed Aug 23, 2006 8:05 am   Post subject: (No subject)

Here you go:
code:
var letter : array 1..26 of string (1)
for something: 'a'..'z'
letter (ord (something)-ord ('a') + 1) := something
end for

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
Sponsor
Sponsor
Sponsor
sponsor
TokenHerbz




PostPosted: Wed Aug 23, 2006 2:38 pm   Post subject: (No subject)

my way is best, as it is far easier to get any characters needed, and just as easy to change them.
Cervantes




PostPosted: Wed Aug 23, 2006 3:10 pm   Post subject: (No 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.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: