"One boy jumped on some girl"
"One boy jumped on some girl"
I tried to make the first letter of each sentence uppercase but think that im on the wrong track. Can anyone point me in the right direction?
Code:#include <stdio.h> #include <stdlib.h> #include <time.h> #include <ctype.h> #include <string.h> #define SIZE 5 void sentenceGenerator(const char *const article[], const char *const noun[], const char *const verb[], const char *const preposition[]) { char firstletter; int i; for ( i = 0; i < 20; i++ ) { sprintf( "%s %s %s %s %s %s.\n", article [rand() % SIZE], noun [rand() % SIZE], verb [rand() % SIZE], preposition [rand() % SIZE], article [rand() % SIZE], noun [rand() % SIZE]); char *strncpy( char *article, const char firstletter, 1 ); toupper( fistletter ); printf( "%s %s %s %s %s %s.\n", article [rand() % SIZE], noun [rand() % SIZE], verb [rand() % SIZE], preposition [rand() % SIZE], article [rand() % SIZE], noun [rand() % SIZE]); } } int main() { const char *article [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 *preposition[SIZE] = { "to", "from", "over", "under", "on",}; srand(time(0)); sentenceGenerator( article, noun, verb, preposition ); return 0; }
Code:void sentenceGenerator(const char *const article[], const char *const noun[], const char *const verb[], const char *const preposition[]) { char sentence[128]; int i; for ( i = 0; i < 20; i++ ) { sprintf( sentence, "%s %s %s %s %s %s.", article [rand() % SIZE], noun [rand() % SIZE], verb [rand() % SIZE], preposition [rand() % SIZE], article [rand() % SIZE], noun [rand() % SIZE]); sentence[0] = toupper( sentence[0] ); puts(sentence); } }
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
You make it seem so easy.
My only question is that in the directions it says that each word should be concatenated to the previous words, do I need to do anything else for this requirement? ( strcat) And if so, whats the purpose if I already get the desired results?
Last edited by jsbeckton; 10-14-2005 at 04:19 PM.
I suppose it depends on who is interpreting the "concatenation" requirement, I'd guess. To me, the format specifier used with sprintf surely concatenates the strings. Concatenating with strcat could accomplish the same thing, too. (But I wouldn't use them both together.)
[edit]I'd imagine a strcat approach would look like this:
But the thought of all those passes back and forth through that poor string via the strcat gives me an incky feeling.Code:strcpy(sentence, article [rand() % SIZE]); sentence[0] = toupper( sentence[0] ); strcat(sentence, " "); strcat(sentence, noun [rand() % SIZE]); strcat(sentence, " "); strcat(sentence, verb [rand() % SIZE]); strcat(sentence, " "); strcat(sentence, preposition [rand() % SIZE]); strcat(sentence, " "); strcat(sentence, article [rand() % SIZE]); strcat(sentence, " "); strcat(sentence, noun [rand() % SIZE]); strcat(sentence, ".");
Last edited by Dave_Sinkula; 10-14-2005 at 04:31 PM.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*