convert alphabet to integers

This is a discussion on convert alphabet to integers within the C Programming forums, part of the General Programming Boards category; hi I'm wanting to convert alphabet to numbers as in a == 1, b == 2. Instead I get asci, ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    16

    convert alphabet to integers

    hi I'm wanting to convert alphabet to numbers as in a == 1, b == 2. Instead I get asci, so i want to subtract 96. like so:
    Code:
    #include<stdio.h>
    #include<ctype.h>
    
    #define MAXSTRING 100
    
    int main()
    {
       char c,name[MAXSTRING];
       int i, sum = 0;
    
       printf("\nhello!   What is your name? ");
       for (i = 0 ;(c = getchar()) != '\n'; ++i){
          name[i] = c;
          if (isalpha(c))
             //sum += c;
              sum += (c - 96);          
       }
       name[i]='\0';
       printf("\n%s%s%s\n%s",
       "hi ", name, ".",
       "Your name spelt backward is ");
       for(--i ; i  >=0 ; --i)
          putchar(name[i]);
       printf("\n%s%d%s\n\n%s\n",
          "and the letters in your name sum to ", sum, ".",
          "have a nice day\n");
     
       return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    c - 'a' should do that.
    Make sure that c is [a-z] though you could use tolower(c) - 'a'

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    16
    so i tried
    sum += (c - 'a');
    Still incorrect answer..Is it something to do with reading a string and omitting end of line character?

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    if c == 'a', then:
    c - 'a' == 0

    So if you want 'a' to equal 1, you need to add 1:
    Code:
    sum += (c - 'a') + 1;
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  5. #5
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Code:
    printf("\n%s%s%s\n%s",
       "hi ", name, ".",
       "Your name spelt backward is ");
    printf isn't that difficult to use, the following code would print the same thing
    Code:
    printf("\nhi %s.\n Your name spelt backward is ", name);
    sum += (c - 'a');
    'a' - 'a' would equal 0, so you need to add 1 to get the values specified in your original post
    try
    Code:
    sum += c - 'a' + 1;
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    16
    thanks. got that now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help assignment due tomorrow
    By wildiv in forum C Programming
    Replies: 6
    Last Post: 01-27-2010, 07:38 PM
  2. Convert array of characters in file to integers
    By millsy5 in forum C Programming
    Replies: 4
    Last Post: 10-02-2009, 11:41 AM
  3. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  4. Convert string of characters to integers
    By Digital_20 in forum C++ Programming
    Replies: 2
    Last Post: 04-02-2008, 09:40 PM
  5. Replies: 6
    Last Post: 08-04-2003, 10:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21