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

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 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

C Program to Find first occurrence of char from a String.

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

 

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

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

Output

occurrence

C Program to Find two Successive Vowels from a String

C Program to Find  two Successive Vowels from a String.Compiled in DEV C++

 

#include<stdio.h>
#include<string.h>
#include<conio.h>
int FindSuccessiveVowel(char [],char []);
int CheckIfVowel(char );
int string_length(char *);
main()
{
        char a[30],b[30];
        int count;
        printf("\n Program to Find two Successive Vowels from a String \n");
        printf("\n-----------------------------------------------------------\n\n\n");
        printf("Enter Text : ");
        gets(a);

        strlwr(a);

        count=FindSuccessiveVowel(a,b);

        printf("\n\nNo of Successive Vowels : %d ",count);

        printf("\n\n\nSuccessive Vowels : %s",b);

        getch();

}

int FindSuccessiveVowel(char source[],char target[])
{
    char *p;
    int i,k=0,count=0;

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

       if((CheckIfVowel(*p)==1)&&(CheckIfVowel(*(p+1))==1))
       {
           count++;
           target[k]=*p;
           k++;
           target[k]=*(p+1);
           k++;
           target[k]=' ';
           k++;
       }

     } 

      target[k]='\0';

      return count;

}                           

int CheckIfVowel(char a)
{
    if((a=='a')||(a=='e')||(a=='i')||(a=='o')||(a=='e')||(a=='u'))
          {
              return 1;                                                           

          }

    return 0;

}        

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

        return count;

     }

 

Output

successive

C Program to Find no of Vowels Consonants and Space from a String

C Program to Find no of Vowels , Consonants and Space from a String. Compiled in DEV C++

 

#include<stdio.h>
#include<string.h>
#include<conio.h>
void Find_Vowels(char *,int *,int *,int *);
main()
{
        char a[30];
        int vowel,Consonants,Space;
        printf("\n Program to Find no of Vowels , Consonants and Space from a String \n");
        printf("\n-----------------------------------------------------------------------\n\n\n");
        printf("Enter Text : ");                                                   
        gets(a);
        
        strlwr(a);
        
        Find_Vowels(a,&vowel,&Consonants,&Space);
        
        printf("\nNo of Vowels : %d ",vowel);
        
        printf("\nNo of Consonants : %d ",Consonants);
        
        printf("\nNo of Space : %d ",Space);
        
        getch();
      
      
}

 void Find_Vowels(char *a,int *vowels,int *consonants,int *space)
     {
        int length=0;
        int v=0,c=0,s=0;
        
        while(*a!='\0')
        {
          if(*a==' ')
          {
              s++;  
          }
          
          if((*a=='a')||(*a=='e')||(*a=='i')||(*a=='o')||(*a=='e')||(*a=='u'))
          {
              v++;                                                           
                                                                                
          }
            
          length++;
          a++;              
        }    
        
        c=length-(v+s);
        
        *vowels=v;
        *consonants=c;
        *space=s;
        
          
     }

 

Output

vowelwithspace

C Program to check if a String Palindrome or not

C Program to check if a String Palindrome or not . Compiled in Dev C++

 

#include<stdio.h>
#include<string.h>
#include<conio.h>
void reverse_string(char [],char []);
int compare_string(char * , char *);
main()
{
        char a[30],b[30];
        int isPalindrome;
        printf("\n Program to Find if a String Palindrome or not Using Pointer\n");
        printf("\n-----------------------------------------------------------------\n\n\n");
        printf("Enter Text : ");
        gets(a);
        strlwr(a);
        reverse_string(a,b);

        isPalindrome=compare_string(a,b);

        if(isPalindrom==1)
        printf("\nString is Palindrome");
        else
        printf("\nString is not Palindrome");

        getch();
        }

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

       }

       return 1;

     }

 void reverse_string(char a[],char b[])

     {
          int i,length,n,k=0;
          char *j;
          n=strlen(a);
          for(i=n-1;i>=0;i--)
          {
             j=&a[i];

             b[k]=*j;

             k++;

          }

          b[k]='\0';   

     }

Output

palindrom

C program to Concat String using pointer

C program to Concat String using Pointer  . compiled in DEV C++

 

 

#include<stdio.h>
#include<string.h>
#include<conio.h>
void concat_string(char a[],char *b);
main()
{
char a[30],b[30];
printf("\n Program to Concat String Using Pointer\n");
printf("\n--------------------------------------------------------\n\n\n");
printf("First Text : ");
gets(a);
printf("\nSecond Text : ");
gets(b);
concat_string(a,b);
printf("\nNew text : %s",a);

getch();
}

void concat_string(char a[],char *b)
{
int length=0;
length=strlen(a);

while(*b!='\0')
{
a[length]=*b;

b++;
length++;

}

a[length]='\0';

}

 

Output

concat

C program to insert an element at the beginning of singly linklist.

Following program will insert an element at the beginning of  singly link list.

//////////////////////////////////////////////////////////////////
  /// -:Insert an element at the beginning of singly link list:- ///
  ////////////////////////////////////////////////////////////
  /*****************************************************************
                 https://wbutassignmentshelp.wordpress.com
  *****************************************************************/

# include<stdio.h>
# include<conio.h>
# include<alloc.h>
# include<stdlib.h>
 struct node
     { int data;
     struct node *link;
     };
     void append(struct node **,int);
     void in_begin(struct node **,int);
     void display(struct node *);
     void main()
     { struct node *p; /* p = start ptr */
     p=NULL;
     int num,loc,c;
     char choice;
     do
     { clrscr();
     printf("PROGRAM TO CREATE SINGLY LINKED LIST AND DISPLAY IT ");
     printf("\n======================================================");
     printf("\n\n1.Create \\ Appending The List");
     printf("\n2.Insert Node At Begining");
     printf("\n3.Displaying the list");
     printf("\n4.Exit");
     printf("\n\nEnter ur Choice : ");
     choice=getch();
     switch(choice)
     {
     case '1':
     char ans;
     do
     {
     printf("How many times you want to enter  : ");
     scanf("%d",&c);
     for(int i=0;i<c;i++)
     {
     printf("Enter any number : ");
     scanf("%d",&num);
     append(&p,num);
     }
     printf("Enter more (y/n) :");
     fflush(stdin);
     ans=getchar();
     }while(ans !='n');
     break;
     case '3':
     display(p);
     getch();
     break;
     case '2':
     printf("Enter The Data : ");
     scanf("%d",&num);
     in_begin(&p,num);
     break;
     case '4':
     clrscr();
     gotoxy(1,10);printf("\n visit https://wbutassignmentshelp.wordpress.com for more program");
     getch();
     exit(0);
     break;

     default:
     printf("Invalid choice.Please Enter Correct Choice");
     getch();

     }
     }while(choice !=7);
     }
         void append(struct node **q,int num)
     { struct node *temp,*r;
     temp = *q;
     if(*q==NULL)
     { temp = (struct node *)malloc(sizeof(struct node));
     temp->data=num;
     temp->link=NULL;
     *q=temp;
     }
     else
     { temp = *q;
     while(temp->link !=NULL)
     { temp=temp->link;
     }
     r = (struct node *)malloc(sizeof(struct node));
     r->data=num;
     r->link=NULL;
     temp->link=r;
     }
     }
     void display(struct node *q)
     { if(q==NULL)
     { printf("\n\nEmpty Link List.Can't Display The Data");
     getch();
     goto last;
     }
     while(q!=NULL)
     { printf("\n%d",q->data);
     q=q->link;
     }
     last:
     }
     int count(struct node *q)
     { int c=0;
     if(q==NULL)
     { printf("Empty Link List.\n");
     getch();
     goto last;
     }
     while(q!=NULL)
     { c++;
     q=q->link;
     }
     last:
     return c;
     }
     void in_begin(struct node **q,int num)
     { struct node *temp;
     if(*q==NULL)
     { printf("Link List Is Empty.Can't Insert.");
     getch();
     goto last;
     }
     else
     { temp=(struct node *)malloc(sizeof(struct node));
     temp->data=num;
     temp->link=*q;
     *q=temp; /* pointing to the first node */
     }
     last:
     getch();
     }