Guest User

Untitled

a guest
Jun 18th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.66 KB | None | 0 0
  1. /****************************************************************
  2. C Primer Plus - Chapter 9, Exercise 6:
  3. ---------------------------------------
  4.  
  5. Write a program that reads characters from the standard input to
  6. end-of-file. For each character, have the program report whether
  7. it is a letter. If it is a letter, also report its numerical
  8. location in the alphabet. For example, c and C would both be
  9. letter 3.
  10.  
  11. Incorporate a function that takes a character as an
  12. argument and returns the numerical location if the character is
  13. a letter and that returns –1 otherwise.
  14.  
  15. ****************************************************************/
  16.  
  17. #include <stdio.h>
  18.  
  19. char letters_in(void);
  20.  
  21. char get_first(void);
  22.  
  23. int main(void)
  24. {
  25.     int input;
  26.     int ans;
  27.    
  28.     input = letters_in();
  29.    
  30.     putchar(input);
  31.     printf(" has the numerical value of %d\n", input, input);
  32.    
  33.     return 0;
  34. }
  35.  
  36. char letters_in(void)
  37. {
  38.      int ch;
  39.      int ans;
  40.      
  41.      ch = get_first();
  42.      
  43.      while ((ch < 'a' || ch > 'z') && (ch < 'A' || ch > 'Z') && ch != EOF)
  44.      {
  45.            printf("Please respond with one of the above choices.\n");
  46.            ch = get_first();
  47.      }
  48.      switch(ch)
  49.           {
  50.                         case 'a' :
  51.                         case 'A' : ans = 1;
  52.                                    break;
  53.                         case 'b' :
  54.                         case 'B' : ans = 2;
  55.                                    break;
  56.                         case 'c' :
  57.                         case 'C' : ans = 3;
  58.                                    break;
  59.                         case 'd' :
  60.                         case 'D' : ans = 4;
  61.                                    break;  
  62.                         case 'e' :
  63.                         case 'E' : ans = 5;
  64.                                    break;
  65.                         case 'f' :
  66.                         case 'F' : ans = 6;
  67.                                    break;
  68.                         case 'g' :
  69.                         case 'G' : ans = 7;
  70.                                    break;
  71.                         case 'h' :
  72.                         case 'H' : ans = 8;
  73.                                    break;
  74.                         case 'i' :
  75.                         case 'I' : ans = 9;
  76.                                    break;
  77.                         case 'j' :
  78.                         case 'J' : ans = 10;
  79.                                    break;
  80.                         case 'k' :
  81.                         case 'K' : ans = 11;
  82.                                    break;
  83.                         case 'l' :
  84.                         case 'L' : ans = 12;
  85.                                    break;
  86.                         case 'm' :
  87.                         case 'M' : ans = 13;
  88.                                    break;
  89.                         case 'n' :
  90.                         case 'N' : ans = 14;
  91.                                    break;
  92.                         case 'o' :
  93.                         case 'O' : ans = 15;
  94.                                    break;
  95.                         case 'p' :
  96.                         case 'P' : ans = 16;
  97.                                    break;
  98.                         case 'q' :
  99.                         case 'Q' : ans = 17;
  100.                                    break;
  101.                         case 'r' :
  102.                         case 'R' : ans = 18;
  103.                                    break;
  104.                         case 's' :
  105.                         case 'S' : ans = 19;
  106.                                    break;
  107.                         case 't' :
  108.                         case 'T' : ans = 20;
  109.                                    break;
  110.                         case 'u' :
  111.                         case 'U' : ans = 21;
  112.                                    break;
  113.                         case 'v' :
  114.                         case 'V' : ans = 22;
  115.                                    break;
  116.                         case 'w' :
  117.                         case 'W' : ans = 23;
  118.                                    break;
  119.                         case 'x' :
  120.                         case 'X' : ans = 24;
  121.                                    break;
  122.                         case 'y' :
  123.                         case 'Y' : ans = 25;
  124.                                    break;
  125.                         case 'z' :
  126.                         case 'Z' : ans = 26;
  127.                                    break;
  128.                         default  : ans = -1;
  129.                                    break;
  130.           }
  131.      return ans;
  132. }
  133.  
  134. char get_first(void)
  135. {
  136.      int ch;
  137.      
  138.      ch = getchar();
  139.      
  140.      while (getchar() != '\n')
  141.            continue;
  142.      
  143.      return ch;
  144. }
Add Comment
Please, Sign In to add comment