-
Display words at random
Hi, im a total newbie in c programming. i got this assignment and dont know how to do it. if anybody could help it would be greatly appreciated.
Code:
char commonWords[13][50] =
{ "It was Thursday.",
"We were looking forward to Friday and the weekend",
"that follows. The wind continued to blow from",
"Sumatra and the air pollution index, API, went up",
"and up. In the afternoon the ministry announced",
"that all schools be closed for the rest of the",
"day as well as Friday. Cancellation of classes",
"has always been the best thing that can ever",
"happen to students and lecturers. On this day,",
"the API told us that it would take a fool to be",
"happy at all. Friday evening, a horrifying sight",
"hit me. Our beautiful pools had turned brownish",
"green."
};
given the code i need to convert them into a 1-dimensional array of single-worded strings at the start of program execution. then generate a random number within the appropriate range and uses that number as the index to the word to be displayed. The word is to be displayed with its letters in reverse order.
example of output :
Code:
Display words at random
--------------------------
Random number: 5
Word at that index: olleh
Please help thank you !!
-
The policy here is to not provide any raw solution to a problem.
You have to work on it, show some code, and ask a specific question about a coding issue you have.
If it's an assignment you surely have some requirement? things you can use or not? You can also start by identifying sub problems you have like reversing a string and printing it, etc.
If you can't do the first part, i.e. splitting the array, then assume you've done it and do the rest.
At least it will give you some hint or the general design of your program.
I suggest you check strtok() to split the string into words, but there are others ways.
-
I think the first bit will be hardest - splitting it into words.
Have a look at strtok - C++ Reference - you can use this to break the sentences into words. Properly, you should do a dry run through the array to find out how many words there are, then create an array of the appropriate size, but you can always do that later and start with a fixed length array.
I like root4's suggestion of starting with something other than the first part. The latter parts seem like they should help you understand what you need to do with the array. Then you can come back with some code :)
-
Welcome saejima! :cool:
Just a small tip: you'll want to include string.h, and stdlib.h into your header files for the program. This program will take some time, so start on it, and post up some code (however humble a start it may be), without delay.