Advertisement
Tkap1

Untitled

Nov 2nd, 2021
788
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.04 KB | None | 0 0
  1.  
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4.  
  5. #define BIG 1024
  6. #define BIGGER 1048576
  7. #define error(...) printf(__VA_ARGS__); exit(1)
  8. #define ZERO {0}
  9.  
  10. void str_capitalized(char* in, char* out, int in_length);
  11.  
  12. int main(void)
  13. {
  14.    
  15.     time_t t;
  16.     srand((unsigned) time(&t));
  17.    
  18.     char** word_arr = malloc(sizeof(char*) * BIGGER);
  19.     int word_count = 0;
  20.    
  21.     // FILE* file = fopen("C:/Users/PakT/Desktop/Dev/Text/frases.txt", "r");
  22.     FILE* file = fopen("frases.txt", "r");
  23.     if(!file)
  24.     {
  25.         error("Couldnt find frases.txt");
  26.     }
  27.     fseek(file, 0, SEEK_END);
  28.     int file_size = ftell(file);
  29.     char* file_data = calloc(1, file_size + 1);
  30.     fseek(file, 0, SEEK_SET);
  31.     int actual_size = fread(file_data, 1, file_size, file);
  32.     fclose(file);
  33.     file_data[actual_size] = '\0';
  34.    
  35.     char* cursor = file_data;
  36.     char* start = cursor;
  37.    
  38.     while(1)
  39.     {
  40.         if(*cursor == '\0')
  41.         {
  42.             int diff = (int)(cursor - start);
  43.             if(diff > 0)
  44.             {
  45.                 char** new_word = word_arr + word_count++;
  46.                 *new_word = calloc(1, 128);
  47.                 memcpy(*new_word, start, diff);
  48.             }
  49.             free(file_data);
  50.             break;
  51.         }
  52.         else if(*cursor == '\n' || *cursor == ' ')
  53.         {
  54.             int diff = (int)(cursor - start);
  55.             if(diff > 0)
  56.             {
  57.                 char** new_word = word_arr + word_count++;
  58.                 *new_word = calloc(1, 128);
  59.                 memcpy(*new_word, start, diff);
  60.             }
  61.             cursor++;
  62.             start = cursor;
  63.         }
  64.         else
  65.         {
  66.             cursor++;
  67.         }
  68.     }
  69.    
  70.    
  71.     while(1)
  72.     {
  73.         char buffer[128] = ZERO;
  74.         fgets(buffer, 128, stdin);
  75.         if(strcmp("exit\n", buffer) == 0)
  76.         {
  77.             exit(0);
  78.         }
  79.         #define min_words 2
  80.         #define max_words 6
  81.         int random_words = min_words + (rand() % (max_words - min_words + 1));
  82.         #undef min_words
  83.         #undef max_words
  84.         for(int i = 0; i < random_words; i++)
  85.         {
  86.             int r = rand() % word_count;
  87.             char capitalized[128] = ZERO;
  88.             str_capitalized(word_arr[r], capitalized, 128);
  89.             printf("%s", capitalized);
  90.         }
  91.         printf("\n");
  92.     }
  93. }
  94.  
  95. void str_capitalized(char* in, char* out, int in_length)
  96. {
  97.     memcpy(out, in, in_length);
  98.     if(in[0] >= 'a' && in[0] <= 'z')
  99.     {
  100.         out[0] -= 'a' - 'A';
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement