Contact Book Application using C
Last Updated :
10 Jun, 2025
A Contact Book is a simple application to store and manage people's contact information such as names and phone numbers. We will create a simple console-based C contact book in this project.
Our contact book application will have following features:
- Add New Contact: Insert name and phone number into the contact book.
- Search Contact: Search a contact using recursion, by name or phone number.
- Display All Contacts: Show all saved contacts.
- Delete Contact: Remove a contact by name or number.
Project Requirements
To do this project, the programmer must be familiar with the following C concepts:
- Decision Making and Loops
- Input/Output and Functions
- Arrays, Strings and Pointers
Additionally, we will need a working C development environment to create and test the application. Refer to this if you want to revise - C Tutorial
Implementation
Let's start working on this project:
STEP 1: Basic Setup
First, we need to create some variables to store the data such that they are accessible to whole program. For this purpose, we use global variables.
- We create two parallel 2d character arrays: names[i] and phones[i], which together represents one contact.
- A count integer that keeps track of how many contacts are currently stored.
C
#include <stdio.h>
#include <string.h>
// Max number of contacts
#define MAX 100
// Parallel arrays to store names and phones
char names[MAX][100];
char phones[MAX][15];
// Number of contacts
int count = 0;
We also created a constant macro MAX to specify the maximum number of contacts it can store.
STEP 2: Creating "Add New Contact" Feature
In this step, we create a function that can add contact to the parallel arrays. While adding contact, we also make sure that the given number is valid.
C
int isValidPhone(const char *phone) {
int len = strlen(phone);
// Remove newline if present (for fgets compatibility)
if (phone[len - 1] == '\n') len--;
// Length must be between 7 and 14
if (len < 7 || len > 14)
return 0;
// Must contain only digits
for (int i = 0; i < len; i++) {
if (phone[i] < '0' || phone[i] > '9')
return 0;
}
return 1;
}
// Add new contact
void addContact() {
if (count >= MAX) {
printf("Contact book is full!\n");
return;
}
printf("Enter name: ");
fgets(names[count], sizeof(names[count]), stdin);
trimNewline(names[count]);
while (1) {
printf("Enter phone number: ");
fgets(phones[count], sizeof(phones[count]), stdin);
trimNewline(phones[count]);
if (isValidPhone(phones[count])) {
break; // valid number
} else {
printf("Invalid phone number! It must be 7-14 digits and contain only numbers.\n");
}
}
printf("Contact added successfully.\n");
count++;
}
The fgets() is used to read the full string (including spaces).
STEP 3: Display All Contacts
We just need to iterate through both the arrays simultaneously and print the contacts one by one.
C
void displayContacts() {
if (count == 0) {
printf("No contacts to display.\n");
return;
}
printf("\n--- Contact List ---\n");
for (int i = 0; i < count; i++) {
printf("%d. Name: %s, Phone: %s\n", i + 1, names[i], phones[i]);
}
}
STEP 4: Search by Name
To find the matching name, we use strcmp() function with the target name and all the names in the names[] array. We have used recursion to implement this, but it can also be implemented using iteration.
C
int searchByName(int index, const char *query) {
if (index == count)
return -1;
if (strcmp(names[index], query) == 0)
return index;
return searchByName(index + 1, query);
}
The extra argument index is used to change the search element in each recursive call.
STEP 5: Recursive Search by Phone
Similar to search by name, the search by phone feature can be implemented.
C
int searchByPhone(int index, const char *query) {
if (index == count)
return -1;
if (strcmp(phones[index], query) == 0)
return index;
return searchByPhone(index + 1, query);
}
STEP 6: Delete Contact by Name
We cannot actually delete an element in arrays, so we have to implement a workaround.
- If the contact is the last element, just decrease the count.
- If the contact is not last element, copy the last contact to its position and then decrease the count.
C
void deleteContact() {
char query[100];
int index;
printf("Enter the name to delete: ");
fgets(query, sizeof(query), stdin);
trimNewline(query);
index = searchByName(0, query);
if (index == -1) {
printf("Contact not found.\n");
return;
}
// If it's the last contact, just reduce count
if (index == count - 1) {
count--;
}
// Else, copy last to current index and reduce count
else {
strcpy(names[index], names[count - 1]);
strcpy(phones[index], phones[count - 1]);
count--;
}
printf("Contact deleted successfully.\n");
}
STEP 7: Dashboard
The final step is to create a dashboard or a menu using which the user can use this application. It should provide options to select all the functionality. You can also add desired cosmetic elements to improve readability and accessibility.
C
int main() {
int choice;
char query[100];
int index;
while (1) {
printf("\n--- Contact Book Menu ---\n");
printf("1. Add Contact\n");
printf("2. Search by Name\n");
printf("3. Search by Phone\n");
printf("4. Display All Contacts\n");
printf("5. Delete Contact\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
getchar(); // clear leftover newline from input buffer
switch (choice) {
case 1:
addContact();
break;
case 2:
printf("Enter name to search: ");
fgets(query, sizeof(query), stdin);
trimNewline(query);
index = searchByName(0, query);
if (index == -1)
printf("Contact not found.\n");
else
printf("Found: Name: %s, Phone: %s\n", names[index], phones[index]);
break;
case 3:
printf("Enter phone number to search: ");
fgets(query, sizeof(query), stdin);
trimNewline(query);
index = searchByPhone(0, query);
if (index == -1)
printf("Contact not found.\n");
else
printf("Found: Name: %s, Phone: %s\n", names[index], phones[index]);
break;
case 4:
displayContacts();
break;
case 5:
deleteContact();
break;
case 6:
printf("Exiting Contact Book. Goodbye!\n");
return 0;
default:
printf("Invalid choice. Try again.\n");
}
}
return 0;
}
In the above menu, an infinite loop is used to make the application run until the user wants to exit.
Execution
Combining each of these elements, we get the executable source code:
C
// Complete Source Code (Click to expand)
//Driver Code Starts
#include <stdio.h>
#include <string.h>
// Max number of contacts
#define MAX 100
// Parallel arrays to store names and phones
char names[MAX][100];
char phones[MAX][15];
// Number of contacts
int count = 0;
// Helper to remove newline character from fgets
void trimNewline(char *str) {
int len = strlen(str);
if (len > 0 && str[len - 1] == '
')
str[len - 1] = '