Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /****************************************************************
- C Primer Plus - Chapter 9, Exercise 6:
- ---------------------------------------
- Write a program that reads characters from the standard input to
- end-of-file. For each character, have the program report whether
- it is a letter. If it is a letter, also report its numerical
- location in the alphabet. For example, c and C would both be
- letter 3.
- Incorporate a function that takes a character as an
- argument and returns the numerical location if the character is
- a letter and that returns –1 otherwise.
- ****************************************************************/
- #include <stdio.h>
- char letters_in(void);
- char get_first(void);
- int main(void)
- {
- int input;
- int ans;
- input = letters_in();
- putchar(input);
- printf(" has the numerical value of %d\n", input, input);
- return 0;
- }
- char letters_in(void)
- {
- int ch;
- int ans;
- ch = get_first();
- while ((ch < 'a' || ch > 'z') && (ch < 'A' || ch > 'Z') && ch != EOF)
- {
- printf("Please respond with one of the above choices.\n");
- ch = get_first();
- }
- switch(ch)
- {
- case 'a' :
- case 'A' : ans = 1;
- break;
- case 'b' :
- case 'B' : ans = 2;
- break;
- case 'c' :
- case 'C' : ans = 3;
- break;
- case 'd' :
- case 'D' : ans = 4;
- break;
- case 'e' :
- case 'E' : ans = 5;
- break;
- case 'f' :
- case 'F' : ans = 6;
- break;
- case 'g' :
- case 'G' : ans = 7;
- break;
- case 'h' :
- case 'H' : ans = 8;
- break;
- case 'i' :
- case 'I' : ans = 9;
- break;
- case 'j' :
- case 'J' : ans = 10;
- break;
- case 'k' :
- case 'K' : ans = 11;
- break;
- case 'l' :
- case 'L' : ans = 12;
- break;
- case 'm' :
- case 'M' : ans = 13;
- break;
- case 'n' :
- case 'N' : ans = 14;
- break;
- case 'o' :
- case 'O' : ans = 15;
- break;
- case 'p' :
- case 'P' : ans = 16;
- break;
- case 'q' :
- case 'Q' : ans = 17;
- break;
- case 'r' :
- case 'R' : ans = 18;
- break;
- case 's' :
- case 'S' : ans = 19;
- break;
- case 't' :
- case 'T' : ans = 20;
- break;
- case 'u' :
- case 'U' : ans = 21;
- break;
- case 'v' :
- case 'V' : ans = 22;
- break;
- case 'w' :
- case 'W' : ans = 23;
- break;
- case 'x' :
- case 'X' : ans = 24;
- break;
- case 'y' :
- case 'Y' : ans = 25;
- break;
- case 'z' :
- case 'Z' : ans = 26;
- break;
- default : ans = -1;
- break;
- }
- return ans;
- }
- char get_first(void)
- {
- int ch;
- ch = getchar();
- while (getchar() != '\n')
- continue;
- return ch;
- }
Add Comment
Please, Sign In to add comment