fork download
  1. /* metajoke.c
  2.  *
  3.  * inspired by this reddit thread:
  4.  * http://w...content-available-to-author-only...t.com/r/Jokes/comments/2q7fb1/multilevel_meta_joke/cn3oqzh
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10.  
  11. const char* meta_pre = "A guy walks into a bar and asks the bartender for a free drink. The bartender says \"I'll give you a free drink if you can tell me a ";
  12. const char* meta_upper2_add = "multi-level ";
  13. const char* meta_upper_add = "meta ";
  14. const char* meta_base_add = "good ";
  15. const char* meta_mid = "joke.\" So the guy says \"";
  16. const char* meta_post = "\" So the bartender gives him a free beer.";
  17. const char* base_joke = "What do you do when you see a spaceman? You park, man.";
  18.  
  19. char* meta_joke(size_t depth) {
  20. const size_t base_joke_size = strlen(base_joke);
  21. const size_t meta_size = strlen(meta_pre) + strlen(meta_mid)
  22. + strlen(meta_post);
  23. const size_t base_meta_size = meta_size + strlen(meta_base_add);
  24. const size_t upper_meta_size = meta_size + strlen(meta_upper_add);
  25. const size_t upper2_meta_size = upper_meta_size + strlen(meta_upper2_add);
  26.  
  27. char* joke;
  28. size_t joke_size = base_joke_size;
  29.  
  30. if (depth > 2) {
  31. joke_size = joke_size + (depth-2) * upper2_meta_size;
  32. }
  33. if (depth > 1) {
  34. joke_size = joke_size + upper_meta_size;
  35. }
  36. if (depth > 0) {
  37. joke_size = joke_size + base_meta_size;
  38. }
  39.  
  40. joke = malloc(joke_size);
  41.  
  42. if (depth > 0) {
  43. strcat(joke, meta_pre);
  44. if (depth > 2) {
  45. strcat(joke, meta_upper2_add);
  46. }
  47. if (depth > 1) {
  48. strcat(joke, meta_upper_add);
  49. } else {
  50. strcat(joke, meta_base_add);
  51. }
  52. strcat(joke, meta_mid);
  53. strcat(joke, meta_joke(depth-1));
  54. strcat(joke, meta_post);
  55. } else {
  56. strcat(joke, base_joke);
  57. }
  58.  
  59. return joke;
  60. }
  61.  
  62. int main() {
  63. /*
  64.   int i;
  65.  
  66.   for (i = 3; i >= 0; i = i - 1) {
  67.   printf("Depth: %d\n%s\n\n", i, meta_joke(i));
  68.   }
  69.   */
  70.  
  71. printf("%s\n", meta_joke(3));
  72.  
  73. return 0;
  74. }
Success #stdin #stdout 0s 2424KB
stdin
Standard input is empty
stdout
A guy walks into a bar and asks the bartender for a free drink. The bartender says "I'll give you a free drink if you can tell me a multi-level meta joke." So the guy says "A guy walks into a bar and asks the bartender for a free drink. The bartender says "I'll give you a free drink if you can tell me a meta joke." So the guy says "A guy walks into a bar and asks the bartender for a free drink. The bartender says "I'll give you a free drink if you can tell me a good joke." So the guy says "What do you do when you see a spaceman? You park, man." So the bartender gives him a free beer." So the bartender gives him a free beer." So the bartender gives him a free beer.