Write a program that uses the random number generator to create sentences. The program should use four arrays of pointers to char called article, noun, verb and preposition. The sentences are constructed in the following order: article, noun, verb, preposition, article, noun. The program should generate 20 sentences. Capitalize the first letter. The arrays should contain:
article "the", "one", "a", "some", "any"
noun "boy", "girl", "dog", "town", "car"
verb "drove", "jumped", "walked", "ran", "skipped"
preposition "to", "from", "over", "under", "on"
Here is the code I have so far:
void sentenceGenerator(const char *const art[], const char
*const noun[],
const char *const verb[], const char *const
prep[])
{
int i;
for (i = 0; i < 20; i++)
{
printf("%s %s %s %s %s %s\n",
art[rand() %
SIZE],
noun[rand() %
SIZE],
verb[rand() %
SIZE],
prep[rand() %
SIZE],
art[rand() %
SIZE],
noun[rand() %
SIZE]);
}
}
int main()
{
const char *art[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 *prep[SIZE] = { "to", "from", "over",
"under", "on", };
srand(time(0));
sentenceGenerator(art, noun, verb, prep);
system("pause");
return 0;
}
How can I get the sentences to capitalize the first letter and put a period at the end?
Modified code is written in bold underline.
#include<stdio.h>
#include<conio.h>
#include <stdlib.h>
#include<time.h>
#include<ctype.h>
#define SIZE 5
void sentenceGenerator( char * art[], char * noun[],
char * verb[], char * prep[])
{
int i;
char*
first;
char
ch;
for (i = 0; i < 20; i++)
{
first=art[rand() %
SIZE];
ch=*first;
//store first
letter in ch
printf("%c",toupper(ch)); //make ch
capital and print
printf("%s ",(first+1)); //print remaining letters in
art
printf(" %s %s %s %s %s\n",
noun[rand() % SIZE],
verb[rand() % SIZE],
prep[rand() % SIZE],
art[rand() % SIZE],
noun[rand() % SIZE]);
}
}
int main()
{
char *art[SIZE] = { "the", "a", "one", "some", "any" };
char *noun[SIZE] = { "boy", "girl", "dog", "town", "car" };
char *verb[SIZE] = { "drove","jumped", "ran", "walked", "skipped"
};
char *prep[SIZE] = { "to", "from", "over", "under", "on" };
srand(time(0));
sentenceGenerator(art, noun, verb, prep);
system("pause");
return 0;
}
Write a program that uses the random number generator to create sentences. The program should use...
Write a script 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 picked, concatenate it to the previous words in the sentence. The words should be separated by spaces. When the final sentence is output, it should start with a capital letter and...
Assignment #6 - Arrays and Strings - Making random sentences! Due: Tuesday April 2, 11:59:00 pm Objective This assignment will consist of writing a program that involves practice with arrays, random number generation, and Strings. You may use c-strings or string objects here, however, string objects is recommended. Exercise Filename: sentences.cpp Write a program that uses random-number generation to create sentences. Create four arrays of strings (string objects highly suggested over c-strings) called article, noun, verb, and preposition. The arrays...
MUST BE PROCEDURAL CODE, DO NOT USE GLOBAL VARIABLES. Write a program in C++that generates random words from a training set as explained in the lectures on graphs. Do not hard-code the training set! Read it from a file. A suggested format for the input file: 6 a e m r s t 10 ate eat mate meet rate seat stream tame team tear Here are some suggestions for constants, array declarations, and helper functions #include <iostream> #include <fstream> #include...
I NEED HELP IN MAZE PROBLEM. Re-write the following program using classes. The design is up to you, but at a minimum, you should have a Maze class with appropriate constructors and methods. You can add additional classes you may deem necessary. // This program fills in a maze with random positions and then runs the solver to solve it. // The moves are saved in two arrays, which store the X/Y coordinates we are moving to. // They are...