# [help]count alphabet

• 09-23-2006
PUI
[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
• 09-23-2006
quzah
Go read the forum guidelines. Pay attention to how to post code, then post your effort.

Quzah.
• 09-23-2006
PUI
sorry for first post
• 09-24-2006
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. :)

• 09-24-2006
PUI
that mean i need 'D' - 'A' = 3 change to ASCII code in my program ?
• 09-24-2006
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. :)

• 09-24-2006
PUI
that means
printf("%c - %c = %d" , first , second , different);
?

seems cant work
• 09-24-2006
bivhitscar
Code:

`printf("%c - %c = %d" , first , second , second - first);`
That should work fine.
• 09-24-2006
PUI
thx for help
• 09-24-2006
PUI
when i type DA it output -3 , how can i change all output to positive number?
• 09-24-2006
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.
• 09-24-2006
PUI
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.

-_-"
• 09-25-2006
bivhitscar
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.