Menu
Coddy logo textTech

Function to Display a Contact

Part of the Logic & Flow section of Coddy's C journey — lesson 53 of 63.

challenge icon

Challenge

Easy

Building on your contact management system from the previous lessons, create a function that displays contact information in a formatted way. This lesson introduces three new string functions you will need: fgets(), strcspn(), and strchr() — each is explained below before the requirements.

New functions used in this lesson:

  • fgets(buffer, size, stdin) — reads a line of input including spaces (unlike scanf, which stops at whitespace). It stores the input into buffer, reading at most size - 1 characters. It also stores the newline character '\n' at the end.
  • strcspn(str, "\n") — returns the index of the first occurrence of '\n' in str. Used to remove the trailing newline left by fgets: str[strcspn(str, "\n")] = '\0';
  • strchr(str, ch) — searches for the character ch in str and returns a pointer to its location, or NULL if not found.

Your program should:

  1. Use the same Contact struct definition from the previous lessons with members:
    • A character array name with size 50
    • A character array phone with size 20
    • A character array email with size 40
    • An integer age
  2. Use the same createContact function from previous lessons that:
    • Dynamically allocates memory for a Contact struct
    • Initializes all members to default values
    • Returns a pointer to the contact
  3. Write a populateContact function that:
    • Takes a pointer to a Contact struct as its parameter
    • Uses fgets() to read each string field (name, phone, email) so that names with spaces are accepted
    • Uses strcspn() to remove the trailing newline from each string field after reading it
    • Uses scanf("%d", ...) to read the integer age field
    • Note: This version of populateContact does not include phone number length validation — do not add it, as the test inputs use short phone numbers like 555-1234
  4. Write a function named displayContact that:
    • Takes a constant pointer to a Contact struct as its parameter: const struct Contact *contactPtr
    • Returns nothing (void)
    • Prints the contact information in this exact format:
      • === CONTACT DETAILS ===
      • Name: [name]
      • Phone: [phone]
      • Email: [email]
      • Age: [age] years old
      • ========================
    • After displaying the basic information, calculates and displays additional details:
      • Calculates the length of the name using strlen() and prints: Name length: [length] characters
      • Determines the generation category based on age and prints:
        • If age is between 0 and 12: Generation: Child
        • If age is between 13 and 19: Generation: Teenager
        • If age is between 20 and 39: Generation: Young Adult
        • If age is between 40 and 59: Generation: Middle-aged Adult
        • If age is 60 or above: Generation: Senior
      • Checks if the email contains the character '@' using strchr() and prints:
        • If '@' is found: Email format: Valid
        • If '@' is not found: Email format: Invalid
  5. In the main function:
    • Create a contact using the createContact function
    • Check if contact creation was successful
    • Call the populateContact function to fill in the contact data
    • Call the displayContact function to show the formatted contact information
    • Free the dynamically allocated memory
    • Print Program completed successfully

This challenge demonstrates how to create a display function that takes a constant pointer to a struct, ensuring that the function cannot modify the original data. You'll practice using fgets() to read strings that contain spaces, strcspn() to strip trailing newlines, the arrow operator to access struct members through a pointer, strchr() for character searching, and conditional logic for data categorization. The use of const in the parameter declaration is a good practice that prevents accidental modification of the contact data within the display function.

Try it yourself

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
    char name[50];
    char phone[20];
    char email[40];
    int age;
} Contact;

Contact* createContact() {
    Contact* contact = (Contact*)malloc(sizeof(Contact));
    if (contact == NULL) {
        return NULL;
    }
    
    strcpy(contact->name, "");
    strcpy(contact->phone, "");
    strcpy(contact->email, "");
    contact->age = 0;
    
    return contact;
}

void populateContact(Contact* contactPtr) {
    printf("Enter name: ");
    scanf("%s", contactPtr->name);
    
    printf("Enter phone: ");
    scanf("%s", contactPtr->phone);
    
    printf("Enter email: ");
    scanf("%s", contactPtr->email);
    
    printf("Enter age: ");
    scanf("%d", &contactPtr->age);
    
    if (contactPtr->age < 0 || contactPtr->age > 120) {
        printf("Invalid age entered\n");
    } else if (strlen(contactPtr->phone) < 10) {
        printf("Invalid phone number\n");
    } else {
        printf("Contact populated successfully\n");
    }
}

int main() {
    Contact* contact = createContact();
    
    if (contact == NULL) {
        return 1;
    }
    
    populateContact(contact);
    
    printf("Contact Information:\n");
    printf("Name: %s\n", contact->name);
    printf("Phone: %s\n", contact->phone);
    printf("Email: %s\n", contact->email);
    printf("Age: %d\n", contact->age);
    
    free(contact);
    printf("Contact deleted successfully\n");
    
    return 0;
}

All lessons in Logic & Flow