Random Sentences generator











up vote
2
down vote

favorite












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.










share|improve this question




























    up vote
    2
    down vote

    favorite












    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.










    share|improve this question


























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      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.










      share|improve this question















      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.







      c array






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 6 '15 at 13:34









      Caridorc

      22.9k536114




      22.9k536114










      asked Mar 13 '13 at 14:06









      Ashish Rawat

      14928




      14928






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          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)






          share|improve this answer

















          • 3




            Well, when you have time please add some explanations.
            – Hugo Dozois
            Mar 14 '13 at 0:01


















          up vote
          1
          down vote













          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;
          }





          share|improve this answer





















            Your Answer





            StackExchange.ifUsing("editor", function () {
            return StackExchange.using("mathjaxEditing", function () {
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            });
            });
            }, "mathjax-editing");

            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "196"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f23845%2frandom-sentences-generator%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            3
            down vote



            accepted










            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)






            share|improve this answer

















            • 3




              Well, when you have time please add some explanations.
              – Hugo Dozois
              Mar 14 '13 at 0:01















            up vote
            3
            down vote



            accepted










            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)






            share|improve this answer

















            • 3




              Well, when you have time please add some explanations.
              – Hugo Dozois
              Mar 14 '13 at 0:01













            up vote
            3
            down vote



            accepted







            up vote
            3
            down vote



            accepted






            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)






            share|improve this answer












            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)







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 13 '13 at 23:40









            John

            461




            461








            • 3




              Well, when you have time please add some explanations.
              – Hugo Dozois
              Mar 14 '13 at 0:01














            • 3




              Well, when you have time please add some explanations.
              – Hugo Dozois
              Mar 14 '13 at 0:01








            3




            3




            Well, when you have time please add some explanations.
            – Hugo Dozois
            Mar 14 '13 at 0:01




            Well, when you have time please add some explanations.
            – Hugo Dozois
            Mar 14 '13 at 0:01












            up vote
            1
            down vote













            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;
            }





            share|improve this answer

























              up vote
              1
              down vote













              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;
              }





              share|improve this answer























                up vote
                1
                down vote










                up vote
                1
                down vote









                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;
                }





                share|improve this answer












                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;
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 15 '13 at 18:28









                William Morris

                8,7671241




                8,7671241






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f23845%2frandom-sentences-generator%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Ellipse (mathématiques)

                    Quarter-circle Tiles

                    Mont Emei