In C language Write a program that will generate a paragraph
In C language!!!
Write a program that will generate a paragraph of 20 random sentences. Each sentence will consist of word types in the following order: article, noun, verb, preposition, article, noun. Use rand() to generate indexes into string tables of various types of words. Use strcat to concatenate these words into a sentence in a large string buffer, along with spaces between words. After you have the sentence in the large buffer, capitalize the first word of the sentence, and strcat a period onto the end. Then print the sentence. Do this for 20 sentences, using a loop. Use the following arrays of character pointers to strings, also known as \"ragged string tables\". What is the advantage of these ragged arrays over \"rectangular string tables\" of the form char table[][80]?
Sample Code:
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 sent[80] = \"\";
strcat(sent, article[rand() % 5]);
Solution
// C program that will generate a paragraph of 20 random sentences
// advantage of these ragged arrays over \"rectangular string tables\" of the form char table[][80]
// is that less space in memory is required and more efficient running time
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include <string.h>
int main()
{
int i;
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\"};
srand(time( NULL ));
for ( i = 0; i < 20; i++ )
{
char sent[80] = \"\";
strcat(sent, article[rand() % 5]);
strcat(sent, \" \");
strcat(sent, noun[rand() % 5]);
strcat(sent, \" \");
strcat(sent, verb[rand() % 5]);
strcat(sent, \" \");
strcat(sent, preposition[rand() % 5]);
strcat(sent, \" \");
strcat(sent, article[rand() % 5]);
strcat(sent, \" \");
strcat(sent, noun[rand() % 5]);
strcat(sent, \"\ \");
sent[0] = toupper( sent[0] );
puts(sent);
}
return 0;
}
/*
output:
The girl walked over one boy
The town walked under one girl
Any car drove over a car
One girl skipped under the car
Any girl walked to one girl
One boy drove over any dog
Some car ran from one car
Any dog jumped to a dog
Any boy drove under the girl
Any dog skipped over one car
A car skipped from a town
The girl skipped over one dog
Some car jumped on any car
Some boy jumped from some dog
One boy skipped from one town
One boy skipped from some dog
A town skipped under the town
One girl ran under one girl
Any girl jumped to one dog
Any girl skipped under any girl
*/


