/*
* greeting.c
* This program reads a first name and a last
name and prints a greeting.
*/
#include <stdio.h>
#include <string.h>
#define MAX_NAME 80
int main ()
{
char first_name[MAX_NAME];
char last_name[MAX_NAME];
char *full_name;
char *greeting;
/* Read the first
name and the last name. */
printf ("Please enter your first name: ");
scanf ("%s", first_name);
printf ("Please enter your last name: ");
scanf ("%s", last_name);
/* Allocate memory for the full name.
Note that we allocate a character for the
blank between the first and
last names, as
well as for the terminating NULL. */
full_name = (char
*) malloc (sizeof(char) *
(strlen(first_name) + strlen(last_name) + 2));
/* Concatenate the first and last name. */
strcpy (full_name,
first_name);
strcat (full_name, " ");
strcat (full_name,
last_name);
/* Allocate sufficient memory for the greeting. */
greeting = (char *) malloc (sizeof(char) * (strlen(full_name) + 60));
/* Create the greeting. */
sprintf (greeting, "Welcome, %s. Your name is %d characters long.",
full_name, strlen(full_name));
/* Print the
greeting. */
printf ("\n<<< %s >>>\n",
greeting);
/* Free memory. */
free (full_name);
free (greeting);
return
(0);
}