Basic c programming help

Discussion in 'Programming' started by benpolska, Jan 24, 2011.

  1. #1
    I am having a problem with the creating the following program:

    Create a program that will read in 2 letters, and then print the character half-way between the two. e.g. given A and G, it will print D. If there are an even number of characters between, then either of the 2 middle ones is OK

    I don't understand how I am suppose to have the program print out the character half-way between the two.

    Here is what I have.

    int main()
    {
    char ch, ch1, ch2;

    printf("Please enter in two characters.\n");
    scanf("%c %c", &ch, &ch1);

    Any help is greatly appreciated.
     
    benpolska, Jan 24, 2011 IP
  2. seafrontsteve

    seafrontsteve Peon

    Messages:
    451
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Cast the characters to integers
    Add the integer values
    Divide by 2
    Then recast back to characters.
    BUT you might want to add a few other details to stop things going wrong - such as first converting all characters to upper or lower case then making sure all values entered are between 'a' and 'z'
     
    seafrontsteve, Jan 24, 2011 IP
  3. kingzzzz

    kingzzzz Guest

    Messages:
    66
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    get the ascII code of both characters
    get the difference between the ascII code
    now divide the difference which you get between the ascII code that is your answer.
     
    kingzzzz, Jan 25, 2011 IP