#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
static char* generate_insult(void){
#define NUM_INSULTS 6
const char *insults[] = {
"manlet",
"incel",
"virgin",
"loser",
"basement dweller",
"jabroni"
};
const unsigned insult_len = (rand() % NUM_INSULTS) + 1;
/* average length (including null terminator) of string literals in array is 8.5 characters */
size_t bytes_allocated = 10 * insult_len + 1;
char *insult_str = malloc(bytes_allocated + 1);
if(!insult_str)
return NULL;
/* make sure first byte is a null terminator */
insult_str[0] = '\0';
for(unsigned i = 0; i < insult_len; ++i){
unsigned idx;
do{
idx = rand() % NUM_INSULTS;
} while(insults[idx] == NULL);
/* expand the allocated memory block if necessary */
if(strlen(insult_str) + strlen(insults[idx]) + 2 > bytes_allocated){
char *ptr = realloc(insult_str, bytes_allocated + strlen(insults[idx]));
if(ptr){
bytes_allocated = bytes_allocated + strlen(insults[idx]);
insult_str = ptr;
} else{
free(insult_str);
return NULL;
}
}
char *offset = insult_str + strlen(insult_str);
sprintf(offset, " %s", insults[idx]);
insults[idx] = NULL;
}
if(strlen(insult_str) + 2 > bytes_allocated){
char *ptr = realloc(insult_str, bytes_allocated + 1);
if(ptr){
++bytes_allocated;
insult_str = ptr;
} else{
free(insult_str);
return NULL;
}
}
size_t offset = strlen(insult_str);
insult_str[offset] = 's';
insult_str[offset + 1] = '\0';
return insult_str;
}
char* generate_HHH_post(void){
#define NUM_EXCUSES 5
char *excuses[NUM_EXCUSES] = {
"you're all hypocrites",
"if Trump did this you'd love it",
"I only respond because you keep tagging me",
"I'm not obsessed, you're obsessed",
"neg rating me proves I'm right"
};
//seed PRNG with current time
srand(time(NULL));
char *insult = generate_insult();
if(!insult)
return NULL;
const size_t insult_len = strlen(insult);
const char *excuse = excuses[rand() % NUM_EXCUSES];
const size_t excuse_len = strlen(excuse);
const size_t post_len = sizeof("A&H posters are just a bunch of") - 1 + insult_len + sizeof(" and ") - 1 + excuse_len + sizeof("!");
char *post_str = malloc(post_len);
if(!post_str){
free(insult);
return NULL;
}
sprintf(post_str, "A&H posters are just a bunch of%s and %s!", insult, excuse);
free(insult);
return post_str;
}
int main(void){
char *HHH_post = generate_HHH_post();
if(!HHH_post){
fprintf(stderr, "Unable to allocate memory!\n");
return -1;
}
printf("%s\n", HHH_post);
free(HHH_post);
return 0;
}