This program use random number generator to create sentences. It prints 20 sentences randomly. Here is the code:
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#define STR_LEN 80
#define MAX_SEN 20
int main(void) {
char *article[] = {"the", "a", "one", "some", "any"};
char *noun[] = {"boy", "girl", "dog", "town", "car"};
char *verb[] = {"drove", "jumped", "ran", "walked", "skipped"};
char *preposition[] = {"to", "from", "over", "under", "on"};
int num;
char sentence[MAX_SEN][STR_LEN];
char (*i)[STR_LEN];
srand((unsigned) time(NULL));
for(i = sentence; i < sentence + MAX_SEN; i++) {
num = rand() % (sizeof(article)/sizeof(article[0]));
strcpy(*i, article[num]);
num = rand() % (sizeof(noun)/sizeof(noun[0]));
strcat(strcat(*i, " "), noun[num]);
num = rand() % (sizeof(preposition)/sizeof(preposition[0]));
strcat(strcat(*i, " "), preposition[num]);
printf("%s.\n", *i);
}
return 0;
}
Can you make some improvements on the code especially on the sizeof
operator. You can use any things functions, arrays, strings, pointers etc.
Also, I don't know how to make first letter capital of each sentence using toupper()
function.