C Tutorial
C Project: Phone Book
Store names and numbers in an array of structs, then search by name.
What you will build
A phone book is an array of contacts. Each contact is a struct with a name and a number. Search walks the array and compares names with strcmp from <string.h>. A match prints the number. No match prints a clear miss.
Names and numbers are hardcoded so the program runs without scanf. Compile withTry it in C at /c/try. That editor is gcc for C.
A contact struct
Two char arrays are enough: one for the name, one for the number. Keep both large enough for the demo strings plus the null terminator. Print the book as a two-column table.
Example
#include <stdio.h>
struct Contact {
char name[24];
char number[16];
};
int main(void) {
struct Contact book[] = {
{"Ada", "555-0100"},
{"Nia", "555-0142"},
{"Omar", "555-0199"},
};
int n = (int)(sizeof book / sizeof book[0]);
for (int i = 0; i < n; i++) {
printf("%-8s %s\n", book[i].name, book[i].number);
}
return 0;
}The initializer copies each string into the field arrays. Do not assign a string with = after the struct already exists; use strcpy if you add a contact later in the program.
strcmp is the name test
strcmp(a, b) returns 0 when the two C strings are equal, including case. A non-zero result means they differ. Never write book[i].name == query. That compares pointers, not letters.
Example
#include <stdio.h>
#include <string.h>
int main(void) {
char stored[] = "Ada";
printf("Ada vs Ada: %d\n", strcmp(stored, "Ada"));
printf("Ada vs ada: %d\n", strcmp(stored, "ada"));
printf("Ada vs Nia: %d\n", strcmp(stored, "Nia"));
return 0;
}The first line should print 0. The others should not. Run this at /c/try.
Search the array
A helper takes the book, the length, and the name to find. It returns the index of the first match, or -1.main runs two queries: one that exists and one that does not. Both results should appear in the output.
Example
#include <stdio.h>
#include <string.h>
struct Contact {
char name[24];
char number[16];
};
int find_name(const struct Contact book[], int n, const char *query) {
for (int i = 0; i < n; i++) {
if (strcmp(book[i].name, query) == 0) {
return i;
}
}
return -1;
}
int main(void) {
struct Contact book[] = {
{"Ada", "555-0100"},
{"Nia", "555-0142"},
{"Omar", "555-0199"},
{"Pia", "555-0115"},
};
int n = (int)(sizeof book / sizeof book[0]);
const char *queries[] = {"Nia", "StudyGrid"};
int qn = 2;
for (int q = 0; q < qn; q++) {
int i = find_name(book, n, queries[q]);
if (i >= 0) {
printf("%s: %s\n", book[i].name, book[i].number);
} else {
printf("%s: not found\n", queries[q]);
}
}
return 0;
}Nia prints 555-0142. StudyGrid prints not found. Search is case-sensitive: nia would miss Nia. That is how strcmp works.
List, then look up
Print the full book first so you can see every record, then run the lookups. This is the complete project shape: data, display, search.
Example
#include <stdio.h>
#include <string.h>
struct Contact {
char name[24];
char number[16];
};
void print_book(const struct Contact book[], int n) {
printf("%-8s %s\n", "name", "number");
for (int i = 0; i < n; i++) {
printf("%-8s %s\n", book[i].name, book[i].number);
}
}
int find_name(const struct Contact book[], int n, const char *query) {
for (int i = 0; i < n; i++) {
if (strcmp(book[i].name, query) == 0) {
return i;
}
}
return -1;
}
void lookup(const struct Contact book[], int n, const char *query) {
int i = find_name(book, n, query);
if (i >= 0) {
printf("lookup %s -> %s\n", query, book[i].number);
} else {
printf("lookup %s -> not found\n", query);
}
}
int main(void) {
struct Contact book[] = {
{"Ada", "555-0100"},
{"Nia", "555-0142"},
{"Omar", "555-0199"},
{"Pia", "555-0115"},
};
int n = (int)(sizeof book / sizeof book[0]);
print_book(book, n);
lookup(book, n, "Omar");
lookup(book, n, "Ada");
lookup(book, n, "Rae");
return 0;
}Common mistakes
- Using
==on twochararrays. Compare withstrcmpand test for 0. - Forgetting
#include <string.h>. Thenstrcmpmay compile as an implicit declaration and behave badly. - Returning 0 for “not found”. Index 0 is a valid contact. Use -1 for a miss.
- Writing a number into a
chararray with%dand no room for digits. Keep numbers as strings in this project, as in555-0100.
Practice
- Search by number instead of name: find who owns
555-0199. - Print every contact whose name starts with the same letter as the query, using the first character only.
- After the table prints, add a fifth hardcoded lookup for
Piaand confirm the number matches the row.