A c program that generates random sentences

A c program that generates random sentences

Posted in: C by musiitwa joseph 2015-03-27 at 16:13:55

image for A c program that generates random sentences The program has 4 character arrays, each with 5 strings. Random numbers are generated from 0 to 5. The generated random numbers are passed as indexes to character arrays hence getting difference strings from the arrays, these are combined to form a sentence in the order: article, noun, verb, preposition, article and noun.

#include<stdio.h>
#include<stdlib.h>
int main(){
/*
the sentence generated should be in the order: article, noun, verb, preposition, article and noun
*/
printf("\n\n\t\tA PROGRAM THAT GENERATES RANDOM SENTENCES \n");

//declaring variables
//holder is an array used store randomly generated numbers
int holder[6], x, i, j=0;

//charcter arrays to be used
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));//changes the algorithm for generating random numbers every time the function is executed
loop://loop is used with the goto statement to come back to this point, this keeps the program running
for(i=0; i<6; i++)
holder[i] = (int)(rand()%5);//generates random numbers between 0 to 5 five times
/*
for example
first time holder[0] = 4
second time holer[1] may be equal to 1
third time holder[2] may be equal to ....
*/

i=0; //resetting i = 0 to be used in the next statements

printf("\n\tSentence %d: ", ++j);//increments j before printing it

//prints setences using the index stored by the array holder
printf("%s ",article[holder[i++]]);//i++ prints with i = 0 and increments it for the next statement
printf("%s ",noun[holder[i++]]);
printf("%s ",verb[holder[i++]]);
printf("%s ",preposition[holder[i++]]);
printf("%s ",article[holder[i++]]);
printf("%s\n",noun[holder[i]]);

//allowing the user to run the program again
again:
printf("\n\tEnter\n\t\t1: To continue.\n\t\t2: To exit.\n\t\t");
scanf("%d",&x); //getting user input
if(x == 1)//checking user input
goto loop;//taking the user back to loop, enables the program to run again
else if(x == 2)
exit(0);
else{//if the user input is not one required
printf("a");//bell
printf("\n\t\tWrong input, Try again\n");
goto again;//taking user back to again
}
return 0;
}

Search

Leave a comment

Fields with * are required
Your email address will not be displayed
1 + 4 =