Run ❯
Get your
own
website
❯
Run Code
Ctrl+Alt+R
Change Orientation
Ctrl+Alt+O
Change Theme
Ctrl+Alt+D
Go to Spaces
Ctrl+Alt+P
#include
#include
#define SIZE 10 struct Contact { char name[20]; char phone[20]; }; struct Contact book[SIZE]; int hash(char name[]) { int sum = 0; for (int i = 0; name[i] != '\0'; i++) { sum += name[i]; } return sum % SIZE; } void add(char name[], char phone[]) { int index = hash(name); while (book[index].name[0] != '\0') { index = (index + 1) % SIZE; } strcpy(book[index].name, name); strcpy(book[index].phone, phone); } int find(char name[]) { int index = hash(name); while (book[index].name[0] != '\0') { if (strcmp(book[index].name, name) == 0) { return index; } index = (index + 1) % SIZE; } return -1; } int main() { add("Liam", "98765432"); add("Anja", "91234567"); add("Ali", "99887766"); int index = find("Ali"); if (index != -1) { printf("Ali's phone number is %s\n", book[index].phone); } return 0; }
Ali's phone number is 99887766