Thread: Getting Value Of A String

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    21

    Getting Value Of A String

    This is probably so easy I could slap myself, but I can't figure out how to do this and it is necessary for a larger program I am building. Assuming that each letter in the alphabet is represented by a value, i.e. A=1, B=2, etc., I need to figure out a way to get the value of the letters of a string by adding up all the letters in it, multiplying those by 3, adding up all the numbers in it, multiplying those by 5, and then adding the two values together. So for example, if I had the string "bad", the value of the string would be 2 + 1 + 4 = 3(7) = 21. If the string was "bad63", the value of the string would be 21 + 5(6 + 3) = 66. Get what I mean? The string itself is coming from an outside file. I'm sure this is a simple thing to do but programming isn't my strong suit and I can't seem to find an answer to my question. Thanks for any help anyone can give me.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Hint:

    sum = 0;
    for ( i = 0 ; i < strlen(string); i++ ) sum += string[i] - 'A' + 1;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    21
    What exactly does the "string[i] - 'A' + 1 do?

    EDIT: Oh never mind, ASCII code. Figured it out. Thanks, that is easy. Now to go bash my head into a wall for not figuring that out on my own. I am in your debt.

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    21
    Wait, does that work for when numbers are part of the string too, or am I going to have to do something extra for that?

  5. #5
    Registered User
    Join Date
    Jul 2005
    Location
    Transcarpathia
    Posts
    49
    there are macros in <ctype.h>, like isdigit() etc.

  6. #6
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Quote Originally Posted by penance
    Wait, does that work for when numbers are part of the string too, or am I going to have to do something extra for that?
    Something extra, but not terribly different.

    Here's a link to an online ASCII chart. Notice the order of numbers, uppercase letters, and lowercase letters. The order is what allows Salem's hint to work. Also notice that numbers, uppercase letters, and lowercase letters have different numeric values (for example, 'A' != 'a').
    Jason Deckard

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare structures
    By lazyme in forum C++ Programming
    Replies: 15
    Last Post: 05-28-2009, 02:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM