fork download
  1. #define META_DEPTH_LEVEL 3
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. 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 ";
  8. const char* meta_upper2_add = "multi-level ";
  9. const char* meta_upper_add = "meta ";
  10. const char* meta_base_add = "good ";
  11. const char* meta_mid = "joke.\" So the guy says \"";
  12. const char* meta_post = "\" So the bartender gives him a free beer.";
  13. const char* base_joke = "What do you do when you see a spaceman? You park, man.";
  14.  
  15. char* meta_joke(int depth) {
  16. const int base_joke_size = strlen(base_joke);
  17. const int meta_size = strlen(meta_pre) + strlen(meta_mid)
  18. + strlen(meta_post);
  19. const int base_meta_size = meta_size + strlen(meta_base_add);
  20. const int upper_meta_size = meta_size + strlen(meta_upper_add);
  21. const int upper2_meta_size = upper_meta_size + strlen(meta_upper2_add);
  22.  
  23. char* joke;
  24. int joke_size = base_joke_size;
  25.  
  26. /* Don't take no negative values. */
  27. if (depth < 0) {
  28. return "No.";
  29. }
  30.  
  31. if (depth > 2) {
  32. joke_size = joke_size + (depth-2) * upper2_meta_size;
  33. }
  34. if (depth > 1) {
  35. joke_size = joke_size + upper_meta_size;
  36. }
  37. if (depth > 0) {
  38. joke_size = joke_size + base_meta_size;
  39. }
  40.  
  41. joke = malloc(joke_size);
  42.  
  43. if (depth > 0) {
  44. strcat(joke, meta_pre);
  45. if (depth > 2) {
  46. strcat(joke, meta_upper2_add);
  47. }
  48. if (depth > 1) {
  49. strcat(joke, meta_upper_add);
  50. } else {
  51. strcat(joke, meta_base_add);
  52. }
  53. strcat(joke, meta_mid);
  54. strcat(joke, meta_joke(depth-1));
  55. strcat(joke, meta_post);
  56. } else {
  57. strcat(joke, base_joke);
  58. }
  59.  
  60. return joke;
  61. }
  62.  
  63. int main() {
  64. printf("%s\n", meta_joke(META_DEPTH_LEVEL));
  65. return 0;
  66. }
Success #stdin #stdout 0s 2380KB
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.