int main() {
int n, found, query_no = 0;
unsigned long int *phone;
char **str, query[30], c;
scanf("%d", &n);
str = (char**) malloc(sizeof(char*) * n); //creating a pointer array of strings
phone = (unsigned long int*) malloc(sizeof(unsigned long int) * n);
for (int i = 0; i < n; i++) {
str[i] = (char*) malloc(sizeof(char) * 30); //iitializing each pointer in arry
}
for (int i = 0; i < n; i++) {
scanf("%s", *(str + i));
scanf("%u", phone + i);
}
gets(query);
char *p;
p = query;
while (*(p = gets(query)) != '\0') //checks if blank line is entered
{
found = 0;
for (int j = 0; j < n; j++) {
/*two pointers are being passed here,*/
/* one to the line entered and */
/* other to each character array pointed to by the pointer array*/
if (strcmp(p, *(str + j)) == 0) {
printf("%s", *(str + j));
printf("=");
printf("%u", *(phone + j));
found = 1;
break;
}
}
if (found == 0) //if record not found, print so
printf("Not found");
printf("\n");
}
The code inputs a phone book from user by first inputting the number of records to be inserted then in each new line a record is entered in the form of name number. Then the user searches for an unknown number records and we print the name and number if the records is found otherwise we print not found.
Source : Hackerrank 30 days of code Day 8 (I hope I am not violating any rules here, please tell me if I am)