Sunday, August 2, 2009

Anyone know how to do this C++? using simple c++ language?

Write a program that determines the letter that lies halfway between two letters of the


alphabet as input by the user. For example, if the user inputs ‘A’ and ‘Z’, the output


should be ‘M’. Do you know how to deal with upper vs. lower case letters?

Anyone know how to do this C++? using simple c++ language?
#include %26lt;stdio.h%26gt;


#include %26lt;ctype.h%26gt;





int main(int argc, char **argv) {


unsigned char c1, c2;





printf("Enter the first char: ");


c1 = toupeer(getc(stdin));


printf("Enter the second char: ");


scanf("%c", %26amp;c2);


c2 = toupper(getc(stdin));





printf("Middle character between %c and %c is %c.\n", c1, c2, (c1 + c2) / 2);


return 0;


}
Reply:I think you guys just answered a coursework question =)
Reply:This code will work if both characters are of the same type. If one is a capital letter and the other is not, then the specification should tell you what to do; it's not normally left up to the coder to make this up....





char first, second, middle;





// Both capital letters, in order


first = 'A';


second = 'Z';


middle = ((int) first + (int) second) / 2;


printf("The character in the middle is '%c'.", middle);





// Both normal letters, NOT in order


first = 'y';


second = 'c';


middle = ((int) first + (int) second) / 2;


printf("The character in the middle is '%c'.", middle);


No comments:

Post a Comment