funct_ptr.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // func_ptr.c -- uses function pointers
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #define LEN 81
  6. char * s_gets(char * st, int n);
  7. char showmenu(void);
  8. void eatline(void); // read through end of line
  9. void show(void (* fp)(char *), char * str);
  10. void ToUpper(char *); // convert string to uppercase
  11. void ToLower(char *); // convert string to uppercase
  12. void Transpose(char *); // transpose cases
  13. void Dummy(char *); // leave string unaltered
  14. int main(void)
  15. {
  16. char line[LEN];
  17. char copy[LEN];
  18. char choice;
  19. void (*pfun)(char *); // points a function having a
  20. // char * argument and no
  21. // return value
  22. puts("Enter a string (empty line to quit):");
  23. while (s_gets(line, LEN) != NULL && line[0] != '\0')
  24. {
  25. while ((choice = showmenu()) != 'n')
  26. {
  27. switch (choice ) // switch sets pointer
  28. {
  29. case 'u' : pfun = ToUpper; break;
  30. case 'l' : pfun = ToLower; break;
  31. case 't' : pfun = Transpose; break;
  32. case 'o' : pfun = Dummy; break;
  33. }
  34. strcpy(copy, line);// make copy for show()
  35. show(pfun, copy); // use selected function
  36. }
  37. puts("Enter a string (empty line to quit):");
  38. }
  39. puts("Bye!");
  40. return 0;
  41. }
  42. char showmenu(void)
  43. {
  44. char ans;
  45. puts("Enter menu choice:");
  46. puts("u) uppercase l) lowercase");
  47. puts("t) transposed case o) original case");
  48. puts("n) next string");
  49. ans = getchar(); // get response
  50. ans = tolower(ans); // convert to lowercase
  51. eatline(); // dispose of rest of line
  52. while (strchr("ulton", ans) == NULL)
  53. {
  54. puts("Please enter a u, l, t, o, or n:");
  55. ans = tolower(getchar());
  56. eatline();
  57. }
  58. return ans;
  59. }
  60. void eatline(void)
  61. {
  62. while (getchar() != '\n')
  63. continue;
  64. }
  65. void ToUpper(char * str)
  66. {
  67. while (*str)
  68. {
  69. *str = toupper(*str);
  70. str++;
  71. }
  72. }
  73. void ToLower(char * str)
  74. {
  75. while (*str)
  76. {
  77. *str = tolower(*str);
  78. str++;
  79. }
  80. }
  81. void Transpose(char * str)
  82. {
  83. while (*str)
  84. {
  85. if (islower(*str))
  86. *str = toupper(*str);
  87. else if (isupper(*str))
  88. *str = tolower(*str);
  89. str++;
  90. }
  91. }
  92. void Dummy(char * str)
  93. {
  94. // leaves string unchanged
  95. }
  96. void show(void (* fp)(char *), char * str)
  97. {
  98. (*fp)(str); // apply chosen function to str
  99. puts(str); // display result
  100. }
  101. char * s_gets(char * st, int n)
  102. {
  103. char * ret_val;
  104. char * find;
  105. ret_val = fgets(st, n, stdin);
  106. if (ret_val)
  107. {
  108. find = strchr(st, '\n'); // look for newline
  109. if (find) // if the address is not NULL,
  110. *find = '\0'; // place a null character there
  111. else
  112. while (getchar() != '\n')
  113. continue; // dispose of rest of line
  114. }
  115. return ret_val;
  116. }