2
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

Sorry for no explanation. Not really sure why I wrote this. Just saw your post, got in the zone, and suddenly I have a chunk of code! Hope it is insightful.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>

const int SEN_LEN = 80;
const int MAX_SEN = 20;

const char* ARTICLES[] = {"the", "a", "one", "some", "any"};
const char* NOUNS[] =  {"boy", "girl", "dog", "town", "car"};
const char* VERBS[] =  {"drove", "jumped", "ran", "walked", "skipped"};
const char* PREPOSITIONS[] =  {"to", "from", "over", "under", "on"};
const int ARTICLES_SIZE = sizeof(ARTICLES)/sizeof(ARTICLES[0]);
const int NOUNS_SIZE = sizeof(NOUNS)/sizeof(NOUNS[0]);
const int VERBS_SIZE = sizeof(VERBS)/sizeof(VERBS[0]);
const int PREPOSITIONS_SIZE = sizeof(PREPOSITIONS)/sizeof(PREPOSITIONS[0]);

char* generateSentence() {
  char* sentence = calloc((SEN_LEN+1), sizeof(char));

  //Build Sentence
  strcat(sentence, ARTICLES[rand()%ARTICLES_SIZE]);

  strcat(sentence, " ");
  strcat(sentence, NOUNS[rand()%NOUNS_SIZE]);

  strcat(sentence, " ");
  strcat(sentence, VERBS[rand()%VERBS_SIZE]);

  strcat(sentence, " ");
  strcat(sentence, PREPOSITIONS[rand()%PREPOSITIONS_SIZE]);

  //Capitalize first letter
  sentence[0] = toupper(sentence[0]);

  return sentence;
}

int main(int argc, char* argv[]) {
    srand(time(NULL));

    for(int i = 0; i < MAX_SEN; i++) {
        char* sentence = generateSentence();
        printf("%s.\n", sentence);
        free(sentence);
    }

    return 0;
}

Requires C99. (-std=c99 on gcc)

\$\endgroup\$
1
  • 3
    \$\begingroup\$ Well, when you have time please add some explanations. \$\endgroup\$ Mar 14 '13 at 0:01
1
\$\begingroup\$

I can't really say there is anything wrong with your code although I really don't like the nested strcat. There are some minor quibbles, such as the opening brace not being in column 0, the parameters missing from main and the stdlib include missing.

However I can offer some alternatives that are not necessarily better, just different. In the code below, I used a typedef to define a sentence type. To my eyes it is easier to handle one-D arrays (ie and array of sentences) that 2-D arrays. But that is just me. I also used a simple index in the loop rather than a pointer to the sentence. - seems more straightforward to me. And I put each array size into a const - in the code it doesn't matter but in something larger you might want the size more than once. I also defined num at the point of first use. I also added some extra vertical spacing to make it clearer, although I probably wouldn't so much in real code.

typedef char sentence[STR_LEN];

int main(int argc, char **argv)
{
    char *article[] = {"the", "a", "one", "some", "any"};
    const size_t n_articles = sizeof article /sizeof article[0];

    char *noun[] = {"boy", "girl", "dog", "town", "car"};
    const size_t n_nouns = sizeof noun /sizeof noun[0];

    char *preposition[] = {"to", "from", "over", "under", "on"};
    const size_t n_prepositions = sizeof preposition /sizeof preposition[0];

    sentence sentences[MAX_SEN];
    srand((unsigned) time(NULL));

    for (int i = 0; i < MAX_SEN; ++i) {
        int num = rand() % n_articles;
        strcpy(sentences[i], article[num]);

        num = rand() % n_nouns;
        strcat(sentences[i], " ");
        strcat(sentences[i], noun[num]);

        num = rand() % n_prepositions;
        strcat(sentences[i], " ");
        strcat(sentences[i], preposition[num]);

        printf("%s.\n", sentences[i]);
    }
    return 0;
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.