Array of strings vs array of pointers?

These are the instructions for my assignment:
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
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!
#include <string> and then declare your arrays like this:
std::string articles[]={"the","a","one","some","any"};

Assuming you declare sentence like this -> std::string sentence;
the strcat calls could be rewritten like this -> sentence+=articles[rand()%5]+" ";

To capitalize the first letter you can just do -> sentence[0]=toupper(sentence[0]);
Then, you can simply do -> std::cout << sentence << "." << std::endl;

PS: Put a srand(time(0)); call first thing inside main to get different 'random' sentences
every time you run your program. Btw, some of the generated sentences are very funny :D
Topic archived. No new replies allowed.