androth

lol.c

Jun 15th, 2021
1,168
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // found somewhere, can't remember who posted this.
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. int wordsinsentence(char **x) {
  8.     int w = 0;
  9.     while (*x) {
  10.         w += 1;
  11.         x++;
  12.     }
  13.     return w;
  14. }
  15.  
  16. int wordsinmono(char ***x) {
  17.     int w = 0;
  18.     while (*x) {
  19.         w += wordsinsentence(*x);
  20.         x++;
  21.     }
  22.     return w;
  23. }
  24.  
  25. int wordsinbio(char ****x) {
  26.     int w = 0;
  27.     while (*x) {
  28.         w += wordsinmono(*x);
  29.         x++;
  30.     }
  31.     return w;
  32. }
  33.  
  34. int wordsinlib(char *****x) {
  35.     int w = 0;
  36.     while (*x) {
  37.         w += wordsinbio(*x);
  38.         x++;
  39.     }
  40.     return w;
  41. }
  42.  
  43. int wordsinlol(char ******x) {
  44.     int w = 0;
  45.     while (*x) {
  46.         w += wordsinlib(*x);
  47.         x++;
  48.     }
  49.     return w;
  50. }
  51.  
  52. int main(void) {
  53.     char *word;
  54.     char **sentence;
  55.     char ***monologue;
  56.     char ****biography;
  57.     char *****biolibrary;
  58.     char ******lol;
  59.  
  60.     //fill data structure
  61.     word = malloc(4 * sizeof *word); // assume it worked
  62.     strcpy(word, "foo");
  63.  
  64.     sentence = malloc(4 * sizeof *sentence); // assume it worked
  65.     sentence[0] = word;
  66.     sentence[1] = word;
  67.     sentence[2] = word;
  68.     sentence[3] = NULL;
  69.  
  70.     monologue = malloc(4 * sizeof *monologue); // assume it worked
  71.     monologue[0] = sentence;
  72.     monologue[1] = sentence;
  73.     monologue[2] = sentence;
  74.     monologue[3] = NULL;
  75.  
  76.     biography = malloc(4 * sizeof *biography); // assume it worked
  77.     biography[0] = monologue;
  78.     biography[1] = monologue;
  79.     biography[2] = monologue;
  80.     biography[3] = NULL;
  81.  
  82.     biolibrary = malloc(4 * sizeof *biolibrary); // assume it worked
  83.     biolibrary[0] = biography;
  84.     biolibrary[1] = biography;
  85.     biolibrary[2] = biography;
  86.     biolibrary[3] = NULL;
  87.  
  88.     lol = malloc(4 * sizeof *lol); // assume it worked
  89.     lol[0] = biolibrary;
  90.     lol[1] = biolibrary;
  91.     lol[2] = biolibrary;
  92.     lol[3] = NULL;
  93.  
  94.     printf("total words in my lol: %d\n", wordsinlol(lol));
  95.  
  96.     free(lol);
  97.     free(biolibrary);
  98.     free(biography);
  99.     free(monologue);
  100.     free(sentence);
  101.     free(word);
  102. }
  103.  
RAW Paste Data