Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #define BIG 1024
- #define BIGGER 1048576
- #define error(...) printf(__VA_ARGS__); exit(1)
- #define ZERO {0}
- void str_capitalized(char* in, char* out, int in_length);
- int main(void)
- {
- time_t t;
- srand((unsigned) time(&t));
- char** word_arr = malloc(sizeof(char*) * BIGGER);
- int word_count = 0;
- // FILE* file = fopen("C:/Users/PakT/Desktop/Dev/Text/frases.txt", "r");
- FILE* file = fopen("frases.txt", "r");
- if(!file)
- {
- error("Couldnt find frases.txt");
- }
- fseek(file, 0, SEEK_END);
- int file_size = ftell(file);
- char* file_data = calloc(1, file_size + 1);
- fseek(file, 0, SEEK_SET);
- int actual_size = fread(file_data, 1, file_size, file);
- fclose(file);
- file_data[actual_size] = '\0';
- char* cursor = file_data;
- char* start = cursor;
- while(1)
- {
- if(*cursor == '\0')
- {
- int diff = (int)(cursor - start);
- if(diff > 0)
- {
- char** new_word = word_arr + word_count++;
- *new_word = calloc(1, 128);
- memcpy(*new_word, start, diff);
- }
- free(file_data);
- break;
- }
- else if(*cursor == '\n' || *cursor == ' ')
- {
- int diff = (int)(cursor - start);
- if(diff > 0)
- {
- char** new_word = word_arr + word_count++;
- *new_word = calloc(1, 128);
- memcpy(*new_word, start, diff);
- }
- cursor++;
- start = cursor;
- }
- else
- {
- cursor++;
- }
- }
- while(1)
- {
- char buffer[128] = ZERO;
- fgets(buffer, 128, stdin);
- if(strcmp("exit\n", buffer) == 0)
- {
- exit(0);
- }
- #define min_words 2
- #define max_words 6
- int random_words = min_words + (rand() % (max_words - min_words + 1));
- #undef min_words
- #undef max_words
- for(int i = 0; i < random_words; i++)
- {
- int r = rand() % word_count;
- char capitalized[128] = ZERO;
- str_capitalized(word_arr[r], capitalized, 128);
- printf("%s", capitalized);
- }
- printf("\n");
- }
- }
- void str_capitalized(char* in, char* out, int in_length)
- {
- memcpy(out, in, in_length);
- if(in[0] >= 'a' && in[0] <= 'z')
- {
- out[0] -= 'a' - 'A';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement