lolamontes69

K+R Exercise7_1

Sep 25th, 2014
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.04 KB | None | 0 0
  1. /* Write a program that converts upper-case to lower or lower-case to upper,
  2.  * depending upon the name it is invoked with, as found in argv[0]
  3.  *
  4.  * example usage of getchar() and putchar()
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <ctype.h>   /* for tolower() toupper() */
  9. #include <string.h>  /* for strcmp() */
  10.  
  11. void errormessage(void);
  12. void utol(void);
  13. void ltou(void);
  14.  
  15.  
  16. main(int argc, char *argv[])
  17. {
  18.     if(argc > 1 && argc < 3) {
  19.         if(strcmp(argv[1], "upper") == 0)
  20.             ltou();
  21.         else if(strcmp(argv[1], "lower") == 0)
  22.             utol();
  23.         else
  24.             errormessage();
  25.     } else
  26.         errormessage();
  27. }
  28.  
  29. /* errormessage: print usage message */
  30. void errormessage(void)
  31. {
  32.     puts("usage: exercise7_1 [ upper | lower ]");
  33. }
  34.  
  35. /* utol: uppercase to lowercase */
  36. void utol(void)
  37. {
  38.     int c;
  39.  
  40.     while((c = getchar()) != EOF)
  41.         putchar(tolower(c));
  42. }
  43.  
  44. /* ltou: lowercase to uppercase */
  45. void ltou(void)
  46. {
  47.     int c;
  48.  
  49.     while((c = getchar()) != EOF)
  50.         putchar(toupper(c));
  51. }
Advertisement
Add Comment
Please, Sign In to add comment