Mr_HO1A

Phonebook

Jun 30th, 2018
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.94 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  struct contact  {
  4.     int number; //Number cannot be more then 8 digits
  5.     char name[50];
  6.  };
  7.  
  8.  int main(){
  9.     int number_of_contacts = 0,index=0;
  10.     printf("Enter Number Of Contacts : ");
  11.     scanf("%d",&number_of_contacts);//Input the length of array
  12.     struct contact contacts[number_of_contacts]; //Define Structure array
  13.     for(index = 0;index<number_of_contacts;index++){
  14.         scanf("%s %ld",&contacts[index].name,&contacts[index].number);
  15.     }
  16.    
  17.     //Searching Part
  18.     char name[50];
  19.     int found = 0,loc;
  20.     printf("Enter Name To Search: ");
  21.     scanf("%s",&name);
  22.     for(index = 0;index<number_of_contacts;index++){
  23.         if(strcmp(name,contacts[index].name) == 0){ //String Compare Function
  24.             found = 1;
  25.             loc = index;
  26.         }
  27.     }
  28.     if(found == 1){
  29.         printf("Index -> %d\n",loc);
  30.         printf("Name -> %s\nNumber -> %d",contacts[loc].name,contacts[loc].number);
  31.     }
  32.     else{
  33.         printf("Contact Not Found -> X ");
  34.     }
  35.    
  36.  }
Add Comment
Please, Sign In to add comment