/* --------------------------------------------------------------------- ** Figure 13.24: Password validation: comparing and copying strings. ** Figure 13.25: The guys and their message. ** Figure 13.26: Composing the love letter. ** --------------------------------------------------------------------- */ #include "tools.h" #define PASSW "LoveForEver" void compose( void ); void main( void ) { string no_go = " is not the password; No Entry! \n"; const int k = strlen( no_go ); char word[80 + k]; /* Buffer for the password and comment. */ printf( "\n Please enter the password: " ); scanf( "%79[^\n]", word ); if (strequal( word, PASSW )) { puts( " Password is correct; go ahead\n" ); compose(); } else { strcat( word, no_go ); /* Join no_go to end of input word */ printf( " %s\n", word ); } } /* ------------------------------------------------------------------ ** Figure 13.25: The guys and their message. */ #define N 4 /* Number of guys using this program */ #define MAXINSERT 15 /* Maximum length of name or petname */ /* ------------------------------------------------------------------ ** Warning: every name must be no larger than MAXINSERT characters. */ const string guys[N] = {"Harold", "Jim", "Jerry", "Charles"}; const string girls[N] = {"Annie", "June", "Leila", "Desiree"}; const string petnames[N] = {"baby", "bug", "lover", "doll"}; #define L 8 /* Number of lines in message array */ #define MAXMESSAGE 80 /* Maximum length of line in message array */ /* ------------------------------------------------------------------ ** Warning: every line must be no larger than MAXMESSAGE characters ** and any insertion must be at the end of the line. */ const string message[L] = { /* Insertions must be at end of line. */ "\n\n ***", ",\n\n I need you! ", "\n Every moment without my xxx", " is empty!\n Let me back into your life, ***", "! Talk to me!\n You will always be my ***", "-xxx", "!\n\n ", "-- @@@" }; #define KISSES "\n XOXOXOX\n " /* --------------------------------------------------------------------- ** Figure 13.26: Composing the love letter. */ void compose( void ) { int k; /* Line counter */ int who; string guy, girl, petname; char line[MAXMESSAGE + MAXINSERT]; puts( " Form Love Letter" ); who = menu_i( " Who needs a letter?", N, guys ); for (k = 0; k < L; ++k) { strcpy (line, message[k]); guy = strstr( line, "@@@" ); girl = strstr( line, "***" ); petname = strstr( line, "xxx" ); if (guy != NULL) strcpy( guy, guys[who] ); else if (girl != NULL) strcpy( girl, girls[who] ); else if (petname != NULL) strcpy( petname, petnames[who] ); else strcat( line, KISSES ); printf( "%s", line ); } puts( "\n" ); }