Aria
2011-05-02 18:49:57 UTC
Write an application that uses random-number generation to create sentences. Use four arrays of strings called article, noun, verb and preposition. Create a sentence by selecting a word at random from each array in the following order: article, noun, verb, preposition, article and noun. As each word is randomly picked, concatenate it to the previous words in the sentence. When the final sentence is output, it should start with a capital letter and end with a period. The application should generate and display 20 sentences.
The article array should contain the articles "the", "a", "one", "some" and "any".
The noun array should contain the nouns "boy", "girl", "dog", "town" and "car".
The verb array should contain the verbs "drove", "jumped", "ran", "walked" and "skipped".
The preposition array should contain the prepositions "to", "from", "over", "under" and "on".
I have this code using an array of pointers
#include "stdafx.h"
#include
#include
#include
#include
#include
#include
using namespace std;
int _tmain()
{
// initialize array of *pointers* //
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" };
char sentence[ 100 ] = ""; //completed sentence//
int i;
for ( i = 1; i <= 20; i++ )
{
//choose random parts of sentence//
strcat( sentence, article[ rand() % 5 ] );
strcat( sentence, " ");
strcat( sentence, noun[ rand() % 5 ] );
strcat( sentence, " " );
strcat( sentence, verb[ rand() % 5 ] );
strcat( sentence, " " );
strcat( sentence, preposition[ rand() % 5 ] );
strcat( sentence, " " );
strcat ( sentence, article[ rand () %5 ] );
strcat( sentence, " " );
strcat( sentence, noun[ rand() % 5 ] );
//capitalize first letter//
putchar( toupper ( sentence [0] ));
//add period at end of sentence//
printf( "%s.\n", &sentence[1] );
sentence[ 0 ] = '\0';
}
return 0;
}
How can i rewrite this code to make it have an array of strings instead of pointers?
Any help/hints will be greatly appreciated!