[help]count alphabet

This is a discussion on [help]count alphabet within the C Programming forums, part of the General Programming Boards category; i want to count the number of alphabet between my input exmaple : I input AD , i want it ...

  1. #1
    PUI
    PUI is offline
    Registered User
    Join Date
    Sep 2006
    Posts
    12

    [help]count alphabet

    i want to count the number of alphabet between my input

    exmaple : I input AD , i want it show me the answer is 3,since
    (A>B>C>D)

    i am newbie for C program , pls help

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Go read the forum guidelines. Pay attention to how to post code, then post your effort.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    PUI
    PUI is offline
    Registered User
    Join Date
    Sep 2006
    Posts
    12
    sorry for first post

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by PUI
    sorry for first post
    No problem, Pui.

    The key to the calculation you want to do, is this - letters have a numbered value called ASCII, so A is one number less than B, which is one number less than C, etc. Capital letters are lower in number than any of the small letters, but the small letters have the same numbered arrangement to their values:

    so a is one less than b, which is one less than c, etc.

    Knowing that relationship exists, we can actually figure out the difference between any two letters, simply by subtracting the smaller of them, from the larger.

    So 'D' - 'A' = 3 (not in C code, but mathematically, using their respective numbers).

    Here's some tips:

    1) you'll want to have a first line of "#include <stdio.h>" so you can do some input, and output in your program.

    2) have an "int main(void)" function
    3) with opening and closing curly braces enclosing it "{ }"
    4) and at the end of your code, a return 0 statement.

    And when you post up your program code, use code tags ALWAYS:

    [*code]
    Cool program here!
    [*/code]

    But remove the "*" in both tags (those help you see the tags, as words, otherwise, you can't see them).

    And welcome to the forum, Pui.

    Adak

  5. #5
    PUI
    PUI is offline
    Registered User
    Join Date
    Sep 2006
    Posts
    12
    that mean i need 'D' - 'A' = 3 change to ASCII code in my program ?

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by PUI
    that mean i need 'D' - 'A' = 3 change to ASCII code in my program ?
    No need. Your computer (if it's a personal computer), already probably uses ASCII, so it knows these values. Just something like: gap = 'D' - 'A'; should be enough.

    Naturally, your program has to have a main function, etc., still.

    Adak

  7. #7
    PUI
    PUI is offline
    Registered User
    Join Date
    Sep 2006
    Posts
    12
    that means
    printf("%c - %c = %d" , first , second , different);
    ?

    seems cant work

  8. #8
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Code:
    printf("%c - %c = %d" , first , second , second - first);
    That should work fine.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  9. #9
    PUI
    PUI is offline
    Registered User
    Join Date
    Sep 2006
    Posts
    12
    thx for help

  10. #10
    PUI
    PUI is offline
    Registered User
    Join Date
    Sep 2006
    Posts
    12
    when i type DA it output -3 , how can i change all output to positive number?

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Subtract the smaller number from the bigger number. Basic math. If A is 1, then Z is ... bigger. Subtract small from big. Big from small gives a negative.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    PUI
    PUI is offline
    Registered User
    Join Date
    Sep 2006
    Posts
    12
    Quote Originally Posted by quzah
    Subtract the smaller number from the bigger number. Basic math. If A is 1, then Z is ... bigger. Subtract small from big. Big from small gives a negative.


    Quzah.
    -_-"

  13. #13
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Quote Originally Posted by PUI
    when i type DA it output -3 , how can i change all output to positive number?
    Try this:

    Code:
    printf("%c - %c = %d" , first , second , abs(second - first));

    You may have to include the math.h header file for this to work, depending on your compiler.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Number or Alphabet
    By Ahmed29 in forum C++ Programming
    Replies: 10
    Last Post: 10-28-2008, 04:50 AM
  2. How to determine a DSL in an Alphabet using C codes
    By sonamjha in forum C Programming
    Replies: 4
    Last Post: 05-19-2008, 05:34 AM
  3. Alphabet not changing
    By swgh in forum C Programming
    Replies: 5
    Last Post: 05-25-2007, 03:32 PM
  4. shorter way to represent alphabet
    By cdkiller in forum C++ Programming
    Replies: 5
    Last Post: 09-30-2006, 12:38 PM
  5. i,j, n,m, x,y,z ... We dont know the alphabet
    By lightatdawn in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-20-2002, 04:46 AM

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