Thread: random sentence generator

  1. #16
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    "One boy jumped on some girl"

  2. #17
    Registered User
    Join Date
    Oct 2005
    Posts
    63
    I tried to make the first letter of each sentence uppercase but think that im on the wrong track. Can anyone point me in the right direction?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <ctype.h>
    #include <string.h>
    #define SIZE 5
    
    void sentenceGenerator(const char *const article[],  const char *const noun[],
                           const char *const verb[], const char *const preposition[])
    {
       char firstletter;
       int i;
       for ( i = 0; i < 20; i++ )
       {
          sprintf( "%s %s %s %s %s %s.\n",
                  article        [rand() % SIZE],             
                  noun           [rand() % SIZE],
                  verb           [rand() % SIZE],
                  preposition    [rand() % SIZE],
                  article        [rand() % SIZE],
                  noun           [rand() % SIZE]);
    
    	  char *strncpy( char *article, const char firstletter,  1 );
          toupper( fistletter );
    
    	  printf( "%s %s %s %s %s %s.\n",
                  article        [rand() % SIZE],             
                  noun           [rand() % SIZE],
                  verb           [rand() % SIZE],
                  preposition    [rand() % SIZE],
                  article        [rand() % SIZE],
                  noun           [rand() % SIZE]);
       }
    }
    
    int main()
    {
       const char *article [SIZE] = { "the", "a", "one", "some", "any",};
       const char *noun[SIZE] = { "boy", "girl", "dog", "town", "car",};
       const char *verb[SIZE] = { "drove","jumped", "ran", "walked", "skipped",};
       const char *preposition[SIZE] = { "to", "from", "over", "under", "on",};
       srand(time(0));
       sentenceGenerator( article, noun, verb, preposition );
       return 0;
    }

  3. #18
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    void sentenceGenerator(const char *const article[],  const char *const noun[],
                           const char *const verb[], const char *const preposition[])
    {
       char sentence[128];
       int i;
       for ( i = 0; i < 20; i++ )
       {
          sprintf( sentence,
                   "%s %s %s %s %s %s.",
                   article        [rand() % SIZE],
                   noun           [rand() % SIZE],
                   verb           [rand() % SIZE],
                   preposition    [rand() % SIZE],
                   article        [rand() % SIZE],
                   noun           [rand() % SIZE]);
    
          sentence[0] = toupper( sentence[0] );
          puts(sentence);
       }
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #19
    Registered User
    Join Date
    Oct 2005
    Posts
    63
    You make it seem so easy.

    My only question is that in the directions it says that each word should be concatenated to the previous words, do I need to do anything else for this requirement? ( strcat) And if so, whats the purpose if I already get the desired results?
    Last edited by jsbeckton; 10-14-2005 at 04:19 PM.

  5. #20
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I suppose it depends on who is interpreting the "concatenation" requirement, I'd guess. To me, the format specifier used with sprintf surely concatenates the strings. Concatenating with strcat could accomplish the same thing, too. (But I wouldn't use them both together.)

    [edit]I'd imagine a strcat approach would look like this:
    Code:
          strcpy(sentence, article        [rand() % SIZE]);
          sentence[0] = toupper( sentence[0] );
          strcat(sentence, " ");
    
          strcat(sentence, noun           [rand() % SIZE]);
          strcat(sentence, " ");
    
          strcat(sentence, verb           [rand() % SIZE]);
          strcat(sentence, " ");
    
          strcat(sentence, preposition    [rand() % SIZE]);
          strcat(sentence, " ");
    
          strcat(sentence, article        [rand() % SIZE]);
          strcat(sentence, " ");
    
          strcat(sentence, noun           [rand() % SIZE]);
          strcat(sentence, ".");
    But the thought of all those passes back and forth through that poor string via the strcat gives me an incky feeling.
    Last edited by Dave_Sinkula; 10-14-2005 at 04:31 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How exactly does the random number generator work?
    By Finchie_88 in forum C++ Programming
    Replies: 6
    Last Post: 08-24-2007, 12:46 AM
  2. Random Sentence From A File
    By gandil in forum C Programming
    Replies: 3
    Last Post: 01-08-2006, 09:50 AM
  3. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 10:56 AM
  4. Testing Random Number Generator
    By Roaring_Tiger in forum C Programming
    Replies: 7
    Last Post: 08-12-2005, 12:48 AM
  5. Random number generator
    By Caze in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2002, 08:10 AM