training

Code I wrote during training
git clone git://git.bitsmanent.org/training
Log | Files | Refs | README

limerick.c (2450B)


      1 /* Exercise 8.12 */
      2 
      3 #include <stdio.h>
      4 #include <string.h>
      5 #include <stdlib.h>
      6 #include <ctype.h>
      7 #include <time.h>
      8 
      9 #define LIMS 5
     10 #define WORDS 5 /* Words for string (article, noun, etc.) */
     11 #define SIZE 30
     12 
     13 int main(void)
     14 {
     15    int i, j, k, x, l, rloop, rhymes[5] = { 0 };
     16    char tmp_s[SIZE], phrase[SIZE];
     17    const char * tokp;
     18 
     19    const char * const article = "the a one some any";
     20    const char * const noun = "boy girl do town car";
     21    const char * const verb = "drove jumped ran walked skipped";
     22    const char * const preposition = "to from over under on";
     23 
     24    /* Store the memory address for each string */
     25    const char * const string[6] = { 
     26       article, noun, verb,
     27       preposition, article, noun
     28    };
     29 
     30    srand( time(NULL) );
     31 
     32    for(l = 0; l < LIMS; l++) { 
     33 
     34       /* loop the phrases */
     35       for(i = 0; i < 5; i++) {
     36 
     37          /* loop the *string[] */ 
     38          for(j = 0; j < 6; j++) {
     39 
     40             /* Create the phrase */
     41 	    rloop = 1 + rand() % WORDS;
     42  
     43             for(k = 0; k < rloop && tokp != NULL; k++) {
     44 	       if(!k) {
     45 	          strcpy(tmp_s, string[j]);
     46 	          tokp = strtok(tmp_s, " ");
     47 	       }
     48 	       else tokp = strtok(NULL, " ");
     49 
     50 	       if(i && j == 5) {
     51 		  /* Check the rhymes */
     52 	          for(x = 0; x < 5; x++) {
     53 	             if(!x) {
     54                         strcpy(tmp_s, string[j]);
     55 		        tokp = strtok(tmp_s, " ");
     56  	  	     }
     57 	   	     else tokp = strtok(NULL, " ");
     58 
     59 		     /* set the rhymes */
     60 		     if(i == 1) {
     61 		        if(tokp[strlen(tokp) - 1] == rhymes[0]) {
     62 		           break;
     63 		        }
     64 		     }
     65 		     else if(i == 2) {
     66 			if(tokp[strlen(tokp) - 1] != rhymes[0])
     67 			   break;
     68 		     }
     69 		     else if(i == 3) {
     70 		       if(tokp[strlen(tokp) - 1] == rhymes[2])
     71   		           break;
     72 		     }
     73 		     else if(i == 4) {
     74 		        if(tokp[strlen(tokp) - 1] == rhymes[0])
     75 			   break;
     76 		     }
     77 
     78 	          }
     79 	       }
     80 	    }
     81 
     82             /* Append the token in the phrase */
     83             if(!j)
     84 	       strcpy(phrase, tokp);
     85 	    else {
     86 	       strcat(phrase, tokp);
     87 	    }
     88 	    strcat(phrase, " ");
     89          }
     90          rhymes[i] = phrase[strlen(phrase) - 2];
     91          strcat(phrase, "\0"); /* "Close" the string */
     92 
     93          /* Add some graphic stuff :-) */
     94          phrase[0] = toupper((int)phrase[0]);
     95          phrase[strlen(phrase) - 1] = '.';
     96 
     97          printf("%2d: %s\n", i + 1, phrase);
     98 
     99       }
    100 
    101       printf("\n");
    102    } /* end for (l) */
    103 
    104    return 0;
    105 } /* E0F main */
    106