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 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 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 length of a String Using Pointers

C program to find the length of a string using Pointer compiled in DEV C++

#include<stdio.h>
#include<string.h>
#include<conio.h>
int string_length(char *);
main()
{
        char a[30];
        printf("\n Program to find Length of a String Using Pointer\n");
        printf("\n--------------------------------------------------------\n\n\n");
        printf("Enter Text : ");
        gets(a);
        printf("\nString Length %d",string_length(a));
        
        getch();
        }
        
        
        
   int string_length(char *a)
     {
          int count=0;
            while(*a!='')
            {
                           count++;
                           a++;
                           }
                           
                           return count;
          
     }




Output

string_length

 

C program to delete an element from an array in any position .

Following code will delete an element from an array in any position.

Compiled with DEV C++

#include <stdio.h>
#include <conio.h>
int main()
{
  int a[50],i,pos,size;
 
  printf(“\nEnter size of the array: “);
  scanf(“%d”,&size);
  printf(“\nEnter %d elements in to the array: “,size);
  for(i=0;i<size;i++)
            scanf(“%d”,&a[i]);
  printf(“\nEnter position where to delete: “);
  scanf(“%d”,&pos);
  i=0;
  while(i!=pos-1)
            i++;
  while(i<10)
  {
            a[i]=a[i+1];
            i++;
  }
  size–;
  printf(“\nAfter implementation”);
  for(i=0;i<size;i++)
            printf(“\n%d”,a[i]);
  getch();
}

Code for Gauss Elimination Method in C

C code to implement  Gauss Elimination  method .  Compiled in DEV C++

 

# include &amp;lt;stdio.h&amp;gt;
# include &amp;lt;conio.h&amp;gt;
int main()
{
int i, j, k, n ;
float a[20][20], x[20] ;
double s, p ;
printf("Enter the number of equations : ") ;
scanf("%d", &amp;amp;n) ;
printf("\nEnter the co-efficients of the equations :\n\n") ;
for(i = 0 ; i &amp;lt; n ; i++)
{
for(j = 0 ; j &amp;lt; n ; j++)
{
printf("a[%d][%d] = ", i + 1, j + 1) ;
scanf("%f", &amp;amp;a[i][j]) ;
}
printf("b[%d] = ", i + 1) ;
scanf("%f", &amp;amp;a[i][n]) ;
}
for(k = 0 ; k &amp;lt; n - 1 ; k++)
{
for(i = k + 1 ; i &amp;lt; n ; i++)
{
p = a[i][k] / a[k][k] ;
for(j = k ; j &amp;lt; n + 1 ; j++)
a[i][j] = a[i][j] - p * a[k][j] ;
}
}
x[n-1] = a[n-1][n] / a[n-1][n-1] ;
for(i = n - 2 ; i &amp;gt;= 0 ; i--)
{
s = 0 ;
for(j = i + 1 ; j &amp;lt; n ; j++)
{
s += (a[i][j] * x[j]) ;
x[i] = (a[i][n] - s) / a[i][i] ;
}
}
printf("\nThe result is :\n") ;
for(i = 0 ; i &amp;lt; n ; i++)
printf("\nx[%d] = %.2f", i + 1, x[i]) ;
getch() ;
}

website_caption1

You might be also interested in :