Page 1 of 1

String$ and Variables?

Posted: Sun Dec 30, 2007 3:37 pm
by Morpheus
:?:I have written a program asking the user to input a word. I have given the letters of the alphabet values from 6 to 156. I would like to add up the values of each letter that was inputed by the user. Then i would like to print the total. If anybody can help me i would appreciate it. This is a very nice web site and alot of info.
Thank you[/i]

Posted: Sun Dec 30, 2007 3:56 pm
by roy
Could you post your program and what you have tried.

Posted: Sun Dec 30, 2007 4:16 pm
by Codemss
First, ask the user to input a word.
I would then make a loop that runs from 1 to the length of the word (LEN(word$)).
In that loop, get the ASCII number of the character at that position(with MID$ and ASC) and add it to an integer variable which is set to 0 before the loop. To get the ascii number of a string: ASC(string$). To get a part of a string$: part$ = MID$(string$, begin, length). If you want one letter, length must be set to 1. Take care: strings don't start at position 0, but at 1.
After the loop, display the variable.
Greets,

Codemss

Re: String$ and Variables?

Posted: Sun Dec 30, 2007 9:30 pm
by Mac
Morpheus wrote:I would like to add up the values of each letter that was inputed by the user.
Please explain why you want that total? What is your application? There may be a better way to do it.

Mac

Posted: Mon Dec 31, 2007 8:57 am
by burger2227
Sounds like Scrabble to me. You can assign each letter your own value in a SELECT CASE routine that reads the word one letter at a time using:

Code: Select all


FOR start = 1 TO LEN(word$)
      letter$ = UCASE$(MID$(word$, start, 1))
      SELECT CASE letter$
         CASE "A": score = 6
         'rest of letters
      END SELECT
      Total = Total + score
NEXT
If you are using consecutive number values for each letter, then you could just use the ASCII codes like Codems said. UCASE the word so that the values are the same for either case key entry. ASC("A") = 65

Ted

Posted: Mon Jan 07, 2008 2:50 am
by burger2227
RESOLVED