PhoneBook Management System using C


#include<stdio.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define FILENAME "PHONEBOOK.PHN"

/* structure of PhoneBook datatype */

struct PhoneBook{

char name[100];
char address[200];
int phoneNo;
char email[100];

};

void mainMenu();                    /*display main menu*/
void addContact(FILE *fp);            /*add new contact*/
void readContacts(FILE *);            /*read contacts from file*/
void deleteContact(FILE *);            /*delete contacts from file*/
void modifyContact(FILE *);            /*modify contacts from file*/
PhoneBook basicDetails(char[]);     /*takes input for one contact object*/
void searchContact(FILE *);            /*search contact from file*/
int checkForMatch(char[],char[]);    /*helper method for searchContact()*/
void menuDesign(char[]);            /*display header */
void showMessage(char[]);             /*display message*/
void pressForMainMenu();             /* returns to main menu */
void fileOpen(FILE *);
void fileClose(FILE *);

main(){

system("cls");
mainMenu();

}

void mainMenu(){

printf("\n\n\n\n\n\t\tLOADING . PLEASE WAIT....");
FILE *fp;
fp=fopen(FILENAME,"rb+");
if(fp==NULL){

fp=fopen(FILENAME,"wb+");
if(fp==NULL){

printf("CANNOT OPEN THE FILE");

}

}

system("cls");
int choise;
char ch;
int result;
printf("\n\n");
menuDesign("PHONEBOOK MANAGMENT SYSTEM");
printf("\n\t\t1. ADD NEW CONTACT");
printf("\n\t\t2. VIEW ALL CONTACTS");
printf("\n\t\t3. SEARCH CONTACTS");
printf("\n\t\t4. MODIFY CONTACTS");
printf("\n\t\t5. DELETE CONTACT");
printf("\n\t\t6. EXIT");

printf("\n\n\n\t\t ENTER YOUR CHOISE : ");
fflush(stdin);
ch=getche();

system("cls");

switch(ch){


case '1' :

addContact(fp);
break;

case '2':

readContacts(fp);
break;

case '3':
searchContact(fp);
break;

case '4':
modifyContact(fp);
break;

case '5':

deleteContact(fp);
break;

case '6':

exit(0);
break;

default:
system("cls");
showMessage("ERROR !! PRESS 1-6 ONLY");
pressForMainMenu();
getch();
}



}

void searchContact(FILE *fp){
typedef PhoneBook CONTACT;
CONTACT ph;
int result;
int count=0;
char item[50];
printf("\n\t\tEnter Some text to be Searched : ");
scanf("%s",&item);
while(fread(&ph,sizeof(ph),1,fp)==1){

result=checkForMatch(ph.name,item);
if(result==1){
count++;
printf("\n \t\t Name : %s \n\t\t Address : %s \n\t\t E-Mail :%s\n\t\t Phone No %d \n\n",ph.name,ph.address,ph.email,ph.phoneNo);
}

}
fclose(fp);
if(count==0)
printf("\n\t\tNothing found for Name : %s",item);
else
printf("\n\t\tTotal %d no of Contact(s) found for  : %s",count,item);

pressForMainMenu();
}

int checkForMatch(char a[],char b[])
{
int i,k;
char *p,temp[strlen(b)];
if(strlen(a)>=strlen(b)){
for(i=0;i<=(strlen(a)-strlen(b));i++)
{
p=&a[i];

k=0;

while(k!=strlen(b))
{
temp[k]=*(p+k);
k++;

}

temp[k]='\0';
//printf("\nOriginal : %s ~~~~~ Temp : %s\n",b,temp);
if(strcmp(strupr(temp),strupr(b))==0)
{
return 1;
}

}
}

return 0;
}


PhoneBook basicDetails(char level[200]){

PhoneBook ph;
menuDesign(level);
printf("\n\t\tEnter Name : ");
scanf("%s",&ph.name);

printf("\n\t\tEnter Address : ");
scanf("%s",&ph.address);


printf("\n\t\tEnter E-Mail : ");
scanf("%s",&ph.email);


printf("\n\t\tEnter phoneNo : ");
scanf("%d",&ph.phoneNo);

return ph;
}

void addContact(FILE *fp){

char another='Y';
PhoneBook ph;
while((another=='Y')||(another=='y')){
system("cls");
ph=basicDetails("ADD NEW CONTACT");
fseek(fp,0,SEEK_END);
fwrite(&ph,sizeof(ph),1,fp);
printf("\n\n\t\t-------------------------------------------------");
printf("\n\t\tAdded Successfully");
printf("\n\n\t\t-------------------------------------------------");
printf("\n\n\t\tWant to add Another Contact ( Y / Any Key ) : ");
fflush(stdin);
another=getche();

}


if(another!='Y'||(another!='y')){
fclose(fp);
system("cls");
mainMenu();
}

}

void readContacts(FILE *fp){

int count=0;
char keyPress;
rewind(fp);
PhoneBook ph;
menuDesign("ALL CONTACTS");
while(fread(&ph,sizeof(ph),1,fp)==1){
count++;
printf("\n \t\t Name : %s \n\t\t Address : %s \n\t\t E-Mail :%s\n\t\t Phone No : %d \n\n",ph.name,ph.address,ph.email,ph.phoneNo);
}
if(count==0){
showMessage("Nothing To Display ..... ");
}
fclose(fp);
pressForMainMenu();

}

void deleteContact(FILE *fp){

system("cls");
menuDesign("DELETE CONTACT");
PhoneBook ph;
FILE *temp;
int count=0;
char name[50];
temp=fopen("TEMP.DAT","wb+");
printf("\n\t\tEnter Contant Name to Delete : ");
scanf("%s",&name);
rewind(fp);
while(fread(&ph,sizeof(ph),1,fp)==1){
if ( strcmp ( ph.name, name ) != 0 ){

fwrite(&ph,sizeof(ph),1,temp);
}else{
count++;
}
}
fclose(fp);
fclose(temp);
remove ( FILENAME ) ;
rename ( "TEMP.DAT", FILENAME ) ;
fp = fopen ( FILENAME, "rb+" ) ;
printf("\n\n\t\t-------------------------------------------------");
if(count!=0)
printf("\n\t\tDeleted Successfully");
else
printf("\n\t\tNo entry found with Name : %s",name);
printf("\n\n\t\t-------------------------------------------------");
fclose(fp);
pressForMainMenu();

}


void modifyContact(FILE *fp){

system("cls");
menuDesign("MODIFY CONTACT DETAILS");
rewind(fp);
int count=0;
PhoneBook ph;
char name[50];
printf("\n\t\tEnter Name to be Modified : ");
scanf("%s",&name);

while(fread(&ph,sizeof(ph),1,fp)==1){
if(strcmp(ph.name,name)==0){
count++;
ph=basicDetails("MODIFY CONTACT ");
fseek(fp,-sizeof(ph),SEEK_CUR) ;
fwrite(&ph,sizeof(ph),1,fp);
break;

}
}
printf("\n\n\t\t-------------------------------------------------");
if(count!=0)
printf("\n\t\tModified Successfully");
else
printf("\n\t\tNo entry found with Name : %s",name);
printf("\n\n\t\t-------------------------------------------------");
fclose(fp);
pressForMainMenu();
}

void menuDesign(char topic[]){
char *pattren;
//pattren=printPattren('=',200);
printf("\n\t\t=============================");
printf("\n\t\t%s",topic);
printf("\n\t\t=============================\n\n");
}

void showMessage(char message[]){


printf("\n\t\t----------------------------------------------");
printf("\n\t\t%s",message);
printf("\n\t\t----------------------------------------------");

}

void pressForMainMenu(){
char keyPress;
printf("\n\n\t\tPress Any Key For Main Menu ... ");
keyPress=getche();
if(keyPress!=' ')
system("cls");
mainMenu();
}


&nbsp;

Screenshorts

PhoneBook

Advertisements

C Program to Replace String from a String

C Program to Replace String from a String .Compiled in Dev C++

#include<stdio.h>
#include<string.h>
#include<conio.h>
void ReplaceString(char [],char [],char [],char []);
main()
{
       char text[50],a[50],b[50],target[50];
      
      
       printf("\nProgram to Replace word from a String \n");
       printf("\n----------------------------------------------\n\n\n");
       printf("Enter Text : ");                                                   
       gets(text);    
       
       printf("\nWord To be Replaced : ");
       gets(a);
       
       printf("\nReplace By : ");
       gets(b);
       
       
       ReplaceString(text,a,b,target);
       
       printf("\nNew String : %s",target);
       
       getch();
       
       
}



void ReplaceString(char source[],char to_be_replaced[],char replaceWith[],char target[])
{
   char *p,temp[30],*t;
   int i,k,compare,pos=0,j,s;
   
   for(i=0;i<strlen(source);i++)
   {
      p=&source[i];
      k=0;
      
     
      while(k!=strlen(to_be_replaced))
      {
        temp[k]=*(p+k);
        k++;                   
      }
      
      
      temp[k]='\0';
      
    
      compare=strcmp(temp,to_be_replaced);
    
     
    
     if(compare==0)
      {
         s=0;        
         while(s!=strlen(replaceWith))
         {
           t=&replaceWith[s];
           target[pos]=*t;
           pos++;  
           s++;  
                   
         }
         
         i=i+strlen(to_be_replaced)-1;
       } 
       
      else
         {          
           target[pos]=*p;
           p++;
           pos++;
         }
         
         
                          
   }//end of for loop       
     
     target[pos]='\0';
}

Output

replace

C Program to Replace Character from a String

C Program to Replace Character from a String .Compiled in Dev C++

#include<stdio.h>
#include<string.h>
#include<conio.h>
void ReplaceCharacter(char [],char ,char );
main()
{
      char text[50],a,b;
       
       
      printf("\nProgram to Replace Character from a String \n");
      printf("\n----------------------------------------------\n\n\n");
      printf("Enter Text : ");                                                   
      gets(text);
      
      
     printf("\n\nEnter the character you want to replace and by what? : ");
     scanf("%c %c",&a,&b);
      
      ReplaceCharecter(text,a,b);
      
      printf("\n\nNew Word is  %s" ,text);
      
      getch();
}

 void ReplaceCharacter(char source[],char to_be_replace,char replacewith)
 {
      char *p;
      int i;
      
      for(i=0;i<strlen(source);i++)
      {
           p=&source[i];
           
           if(*p==to_be_replace)
           {
                source[i]=replacewith;
                                  
           }
           
      }
      
 }
       

Output

replacechar

C Program to Extract String from a String

C Program to Extract String from a String .Compiled in Dev C++

#include<stdio.h>
#include<string.h>
#include<conio.h>
int subString(char [],char [],int ,int );
main()
{
      char text[50],target[50];
      int start,end,isError;
      
      printf("\nProgram to Extract String from a String \n");
      printf("\n----------------------------------------------\n\n\n");
      printf("Enter Text : ");                                                   
      gets(text);
      
     
      
      printf("\nStart Position : ");
      scanf("%d",&start);
      
      printf("\n\nINFO : Enter -1 if you don't want end position \n\n");
      
      printf("\nEnd Position : ");
      scanf("%d",&end);
      
      isError=subString(text,target,start,end);
      
      if(isError==0)
      printf("\nERROR !!! End position can not be less than Start position ");
      else
      printf("\nExtracted String is : %s",target);
      
      getch();
      
}

int subString(char source[],char target[],int start,int end)
{
     char *p;
     int i,k=0;
     
     if((end<start)&&(end!=-1))
     return 0;
     
     if(end==-1)
     end=strlen(source);
     
                
     for(i=start;i<=end;i++)
     {
                    p=&source[i];
                    target[k]=*p;
                    k++;         
     }
     
     target[k]='\0';
     
}

Output

substring

C Program to remove repeated characters from a String

C Program to remove repeated characters from a String  . Compiled in Dev C++

#include<stdio.h>
#include<string.h>
#include<conio.h>
void printFirstChar(char [],char []);
int checkifExist(char ,char *);
main()
{
       char text[50],target[50];
      
      
       printf("\nProgram to remove repeated  characters from a String \n");
       printf("\n------------------------------------------------------------------\n\n\n");
       printf("Enter Text : ");                                                   
       gets(text);  
       printFirstChar(text,target);
       
       printf("\n\New Word after removing repeated  Characters is  : %s" ,target);
        
    
       getch();
               
      
}


 void printFirstChar(char a[],char b[])
     {
        char *p;
        int i,isExits,pos=0;
        for(i=0;i<strlen(a);i++)
        {
            p=&a[i];
            isExits=checkifExist(*p,b);
            if(isExits==0) 
            { 
               b[pos]=*p;
               pos++;
            }                 
                                  
        } 
        
        b[pos]='\0';
        
        
     }
        
        
      int checkifExist(char k,char *b)
      {
          while(*b!='\0')
          {
            if(*b==k)
            {
              return 1;
              break;         
            }
                          
           b++;
           
          }
          
          return 0;
          
      }

Output

removerepitation

C Program to Find Biggest and Smallest word from a String

C Program to Find Biggest and Smallest word from a String . Compiled in Dev C++

#include<stdio.h>
#include<string.h>
#include<conio.h>
#include <ctype.h>
void copyData(char *,char *);
void findSamallestAndBigestWord(char *,char [],char []);
main()
{
       char text[50],big[50],small[50];
       int position,start;
      
       printf("\nProgram to Find Biggest and Smallest word from a String \n");
       printf("\n------------------------------------------------------------------\n\n\n");
       printf("Enter Text : ");                                                   
       gets(text);    
       
       findSamallestAndBigestWord(text,big,small);
       
       printf("\n\nBiggest Word is : %s" ,big);
        
       printf("\n\nSmallest Word id : %s" ,small);
        
    
       getch();
       
}
       
       
                                 
 void findSamallestAndBigestWord(char a[],char MAX[],char MIN[])
  {
      char *p,tempMAX[30],tempMIN[30];
      int i,max=0,min,pos=0;
      if(isspace(a[strlen(a)-1]))
      {}
      else{
      a[strlen(a)]=' ';
      a[strlen(a)]='\0';
      }
      min=strlen(a);
      for(i=0;i<strlen(a);i++)
      {
            p=&a[i];
           
            if(*p!=' ')
            {
                   tempMAX[pos]=*p; 
                   tempMIN[pos]=*p;
                   pos++;
                          
            }
             else{
                 
            tempMAX[pos]='\0';
            tempMIN[pos]='\0';
            
            
            if(strlen(tempMAX)>max)
            {
                copyData(tempMAX,MAX); 
                max=strlen(tempMAX);              
            }
            if(strlen(tempMIN)<min)
            {
                copyData(tempMIN,MIN);
                min=strlen(tempMIN);                      
            }
            
             pos=0;
            
            }
            
             
      }
            
                    
  }
  
  
  void copyData(char *source,char *target)
  {
       while(*source!='\0')
       {
          *target=*source;
          *source++;
          *target++;
       }
       
      *target='\0';
      
  }   

Output

biggest

C Program to Abbreviate a String

C Program to Abbreviate a String . Compiled in Dev C++

#include<stdio.h>
#include<string.h>
#include<conio.h>
void string_abbreviation(char [],char []);
main()
{
      char text[30],target[30];
      printf("\nProgram to Abbreviate a String \n");
      printf("\n--------------------------------\n\n\n");
      printf("Enter Text : ");                                                   
      gets(text);
      
      string_abbreviation(text,target);
      
      printf("\nAbbreviation of string '%s' is : %s",text,target);
      
      getch();
      
}


          
void string_abbreviation(char a[],char b[])
     {
       char *p;
       int length,i,k=0;
       length=strlen(a);
       
       b[k]=a[0];
       k++;
      
       
       for(i=0;i<length;i++)
       {
            p=&a[i];
            
          
            
            if(*p==' ')
            {
                
                b[k]=*(p+1);  
                k++;         
            }            
           
           
                              
        }  
        
        b[k]='\0';
        strupr(b);
          
     }

Output

abbrevviation

C Program to Find first occurrence of char with starting position

C Program to Find first occurrence of char with starting position.Compiled in Dev C++

#include<stdio.h>
#include<string.h>
#include<conio.h>
int First_Occurrence_Char(char [],char ,int);
main()
{
      char text[30],searchChar;
      int position,start;
      
       printf("\nProgram to Find first occurrence of char with starting position \n");
       printf("\n------------------------------------------------------------------\n\n\n");
       printf("Enter Text : ");                                                   
       gets(text);
       
       printf("\nSearched Char : ");
       scanf("%c",&searchChar);
       
       printf("\nStart From : ");
       scanf("%d",&start);
       
       
       position=First_Occurrence_Char(text,searchChar,start);
      
       printf("\nCharecter '%c'  in String '%s' starting at %d found at position %d ",searchChar,text,start,position);
       
       
       getch();
       
} 
      
           
int First_Occurrence_Char(char a[],char b,int start)

{
     
     int i;
     char *p;
     
     for(i=start;i<strlen(a);i++) 
     {
         p=&a[i];       
      
        
         
         if(*p==b)
         {
                  
                    break;
                 
         }
               
                              
     }    
     if(i==strlen(a))
     {
       return -1;                
     }
     else
     {
         return i;
     }
     
}

Output

occurrencewithstart

C Program to Find First occurrence of String from a String

C Program to Find First occurrence of String from a String.Compiled in Dev C++

#include<stdio.h>
#include<string.h>
#include<conio.h>
int compare_string(char * , char *);
int First_Occurrence_String(char [],char []);
int string_length(char *);
main()
{

      char text[30],sreachString[30];
      int position;

      printf("\nProgram to Find First occurrence of String from a String \n");
      printf("\n-----------------------------------------------------------\n\n\n");
      printf("Enter Text : ");
      gets(text);

      printf("\nSearched String : ");
      gets(sreachString);

      position=First_Occurrence_String(text,sreachString);

      printf("\nString '%s' starts in String '%s' from Position %d",sreachString,text,position);

      getch();

      }



int First_Occurrence_String(char a[],char b[])
{
 int i,k;
 char *p,temp[string_length(b)];

 for(i=0;i<=(string_length(a)-string_length(b));i++)
 {
    p=&a[i];

    k=0;

    while(k!=string_length(b))
    {
      temp[k]=*(p+k);
      k++;

    }
    temp[k]='\0';

    if(compare_string(temp,b)==1)
    {
     break;
    }

 }
 if(i==(string_length(a)-string_length(b))+1)
 return -1;
 else
 return i;
}     





int compare_string(char *first , char *second)
     {
       while(*first!='\0')
       {
           if(*first==*second)
              {
                  first++;
                  second++;
               }
                else
              {
               return 0;
              }

       }

       return 1;

     }




 int string_length(char *a)
     {
          int count=0;
            while(*a!='\0')
            {
                           count++;
                           a++;
                           }

          return count;

     }     

Output

occurrence_STRING

C Program to Find Last occurrence of char from a String

C Program to Find Last occurrence of char from a String.Compiled in DEV C++

#include<stdio.h>
#include<string.h>
#include<conio.h>
int Last_Occurrence_Char(char [],char );
main()
{
      char text[30],searchChar;
      int position;
      
       printf("\nProgram to Find Last occurrence of char from a String \n");
       printf("\n-----------------------------------------------------------\n\n\n");
       printf("Enter Text : ");                                                   
       gets(text);
       
       printf("\nSearched Char : ");
       scanf("%c",&searchChar);
       
       position=Last_Occurrence_Char(text,searchChar);
      
       printf("\nCharecter '%c' Found in String '%s' at Position %d",searchChar,text,position);
       
       
       getch();
       
} 
      

     
int Last_Occurrence_Char(char a[],char b)

{
     
     int i;
     char *p;
     
     for(i=strlen(a);i>=0;i--) 
     {
         p=&a[i];       
      
        
         
         if(*p==b)
         {
                  
                    break;
                 
         }
               
                              
     }    
     if(i==strlen(a))
     {
       return -1;                
     }
     else
     {
         return i;
     }
     
}  

Output

lastoccurrence