Skip to content
C project #12Intermediate

File-Based Contact Manager

Validated phone and email entry, always-sorted contacts, case-insensitive search, editing, city filters and tab-separated storage.

Estimated time
~5 hours to build
Files
5 files · 408 lines
Technologies
C11, <ctype.h>, Text file I/O

Introduction

A contact manager is a small application with many details to get right: validation that rejects bad data without frustrating the user, sorting that ignores case, and a storage format that survives commas and spaces.

Problem statement

Store contacts (name, phone, email, city) in a file. Validate Indian mobile numbers (optionally with +91) and email addresses, keep the list sorted by name ignoring case, prevent duplicate names, search across name, phone and email, edit and delete contacts, and list all contacts in a given city.

Objectives

  • Write validation functions for real-world formats
  • Maintain a sorted dynamic array
  • Implement case-insensitive compare and search
  • Use tab-separated text for robust storage

Features

  • Phone and email validation with re-prompting
  • Case-insensitive sorting, duplicate detection and search
  • Edit and delete by exact name
  • Filter by city
  • Tab-separated file storage

Concepts demonstrated

  • Input validation
  • Sorting
  • String processing
  • Dynamic memory

System architecture

A single module keeps contacts in a heap array that is re-sorted with qsort after each insertion. icmp/icontains implement case-insensitive comparison. The file stores one contact per line with tab separators, parsed back with %[^\t] scansets.

Folder structure

TextProject tree
contact-manager/
├── README.md
├── Makefile
├── src/main.c
├── tests/expected-output.txt
└── tests/sample-input.txt

Complete source code

Makefile

20 lines
MakefileMakefile
# Generated for KENHOLT Skills. Usage: make | make test | make clean
CC = gcc
CFLAGS = -std=c11 -Wall -Wextra -O2
SRC := $(wildcard src/*.c)
HDR := $(wildcard src/*.h)
ARGS := $(shell cat tests/args.txt 2>/dev/null)
INPUT := $(wildcard tests/sample-input.txt)
app: $(SRC) $(HDR)
$(CC) $(CFLAGS) -Isrc $(SRC) -o app -lm
test: app
./app $(ARGS) $(if $(INPUT),< $(INPUT),) > actual.txt
diff actual.txt tests/expected-output.txt && echo "Test passed: output matches tests/expected-output.txt"
clean:
rm -f app app.exe actual.txt
.PHONY: test clean

What each file does

src/main.c
Validation (valid_phone, valid_email), storage, sorting, search and the menu.
Makefile
Build automation: make builds ./app, make test runs the sample session and compares it with the expected output, make clean removes build files.

How to run it

Build

Terminal
gcc -std=c11 -Wall -Wextra -Isrc src/*.c -o app -lm
# or simply: make

Run

Terminal
./app # Linux / macOS
.\app.exe # Windows

Test against the expected output

Terminal
./app < tests/sample-input.txt > actual.txt
diff actual.txt tests/expected-output.txt # no output means they match
# Windows PowerShell:
# Get-Content tests\sample-input.txt | .\app.exe > actual.txt
# Compare-Object (Get-Content actual.txt) (Get-Content tests\expected-output.txt)

Expected output

Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.

Texttests/expected-output.txt
Contact Manager - 0 contact(s) loaded.
1.Add 2.List 3.Search 4.Edit 5.Delete 6.Contacts by city 0.Save & exit
Choice:
Name:
Phone (10 digits):
Email:
City:
Added Ravi Kumar.
1.Add 2.List 3.Search 4.Edit 5.Delete 6.Contacts by city 0.Save & exit
Choice:
Name:
Phone (10 digits):
Invalid phone number, try again.
Phone (10 digits):
Email:
Invalid email address, try again.
Email:
City:
Added Anita Desai.
1.Add 2.List 3.Search 4.Edit 5.Delete 6.Contacts by city 0.Save & exit
Choice:
Name:
Phone (10 digits):
Email:
City:
Added meera iyer.
1.Add 2.List 3.Search 4.Edit 5.Delete 6.Contacts by city 0.Save & exit
Choice:
1 Anita Desai 9123456780 anita.desai@mail.com Pune
2 meera iyer +918008001234 meera@kenholt.in hyderabad
3 Ravi Kumar 9876543210 ravi@example.com Hyderabad
1.Add 2.List 3.Search 4.Edit 5.Delete 6.Contacts by city 0.Save & exit
Choice:
Search (name, phone or email):
2 meera iyer +918008001234 meera@kenholt.in hyderabad
1 match(es).
1.Add 2.List 3.Search 4.Edit 5.Delete 6.Contacts by city 0.Save & exit
Choice:
City:
2 meera iyer +918008001234 meera@kenholt.in hyderabad
3 Ravi Kumar 9876543210 ravi@example.com Hyderabad
2 contact(s) in Hyderabad.
1.Add 2.List 3.Search 4.Edit 5.Delete 6.Contacts by city 0.Save & exit
Choice:
Exact name to delete:
Deleted.
1.Add 2.List 3.Search 4.Edit 5.Delete 6.Contacts by city 0.Save & exit
Choice:
1 Anita Desai 9123456780 anita.desai@mail.com Pune
2 meera iyer +918008001234 meera@kenholt.in hyderabad
1.Add 2.List 3.Search 4.Edit 5.Delete 6.Contacts by city 0.Save & exit
Choice:
Saved 2 contact(s) to contacts.txt.
Input
1
Ravi Kumar
9876543210
ravi@example.com
Hyderabad
1
Anita Desai
12345
9123456780
anita.desai@mail
anita.desai@mail.com
Pune
1
meera iyer
+918008001234
meera@kenholt.in
hyderabad
2
3
IYER
6
Hyderabad
5
Ravi Kumar
2
0

Testing

  • Diff with tests/expected-output.txt (includes rejected phone and email entries).
  • Add "zara" then "Aditi" and check that the list stays alphabetical regardless of case.
  • Restart and confirm contacts are reloaded.

Common errors

Contacts with spaces in the city are cut off

The last field is read with %31[^\n], which keeps spaces; check you didn't change it to %s.

Search misses "Iyer" when typing "iyer"

Use the case-insensitive icontains, not strstr, for names and emails.

Possible improvements

  • Import/export vCard (.vcf)
  • Multiple phone numbers per contact
  • Group contacts with tags

Additional challenges

  1. 1Add fuzzy search that tolerates one typo (Levenshtein distance ≤ 1).
  2. 2Detect duplicate phone numbers across different names.
  3. 3Replace the array with a binary search tree keyed by name.

Built and tested it yourself? Mark it to update your dashboard.