CS3271 - C Program Lab Manual
CS3271 - C Program Lab Manual
a) I/O STATEMENTS
Date:
Aim:
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
int main()
{
int rad;
float PI = 3.14, area, ci;
printf("\nEnter radius of circle: ");
scanf("%d", &rad);
area = PI * rad * rad;
printf("\nArea of circle : %f ", area);
ci = 2 * PI * rad;
printf("\nCircumference : %f ", ci);
getch();
}
Output:
Algorithm:
Program:
#include <stdio.h>
#include <conio.h>
int main()
{
int x, y, t;
printf("Enter two integers: ");
scanf("%d%d", &x, &y);
printf("\nBefore Swapping\nFirst integer = %d\nSecond integer = %d\n", x, y);
t = x;
x = y;
y = t;
printf("\nAfter Swapping\nFirst integer = %d\nSecond integer = %d\n", x, y);
getch();
}
Output:
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
int main()
{
int n;
printf("Enter a number : ");
scanf("%d",&n);
printf("\nSquare of the number %d is %d",n,n*n);
printf("\n\nCube of the number %d is %d",n,n*n*n);
getch();
}
Output:
Program:
#include <stdio.h>
#include<conio.h>
int main()
{
float cel,c2,f1,faren;
printf("Enter the temperature in Celsius : ");
scanf("%f",&cel);
f1=cel*9/5+32;
printf("The temperature in fahrenheit is : %.2f\n",f1);
printf("\nEnter the temperature in Fahrenheit : ");
scanf("%f",&faren);
c2=(faren-32)*5/9;
printf("the temperature in celsius is :%.2f",c2);
getch();
}
Output:
Program:
#include <stdio.h>
#include<conio.h>
main()
{
char name[25];
int idno;
float salary;
printf("Enter ID no, Name, Salary : \n");
scanf("%d%s%f",&idno,name,&salary);
printf("\n ID number :%d",idno);
printf("\n Name : %s",name);
printf("\n Salary : %.2f",salary);
getch();
}
Output:
Algorithm:
Program:
#include<stdio.h>
#include<math.h>
#include<conio.h>
int main()
{
double number, result;
printf(" \n Enter a Number to find Square root : ");
scanf("%lf", &number);
result = sqrt(number);
printf("\n Square Root a number is %.2lf",result);
getch();
}
Output:
Result:
Ex. No. 1. b) OPERATORS
Date:
Aim:
i) Arithmetic Operators
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int a,b;
printf("Enter the value of a : ");
scanf("%d",&a);
printf("Enter the value of b : ");
scanf("%d",&b);
printf("\nAddition : a + b : %d\n",a+b);
printf("Subtraction : a - b : %d\n",a-b);
printf("Multiplication : a * b : %d\n",a*b);
printf("Division : a / b : %d\n",a/b);
printf("Remainder : a mod b : %d",a%b);
getch();
}
Output:
ii) Bitwise Operators
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int a,b;
printf("Enter the value of a: ");
scanf("%d",&a);
printf("Enter the value of b: ");
scanf("%d",&b);
printf("\nBitwise not ~a: %d\n",~a);
printf("Bitwise right shift a>>b: %d\n",a>>b);
printf("Bitwise left shift a<<b: %d\n",a<<b);
printf("Bitwise and a&b: %d\n",a&b);
printf("Bitwise xora^b: %d\n",a^b);
printf("Bitwise or a|b: %d",a|b);
getch();
}
Output:
iii) Increment/Decrement operators
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int a;
printf("Enter the numebr : ");
scanf("%d",&a);
printf("\nPre increment ++a: %d\n",++a);
printf("Pre decrement --a: %d\n",--a);
printf("Post increment a++: %d\n",a++);
printf("Post decrement a--: %d",a--);
getch();
}
Output:
iv) Conditional Operator
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,greatest;
printf("Enter the value of a : ");
scanf("%d",&a);
printf("Enter the value of b : ");
scanf("%d",&b);
greatest=a>b?a:b;
printf("\n Using Conditional Operator a>b?a:b, Greatest number is : %d\n",greatest);
getch();
}
Output:
Result:
Thus the C programs using Operators were written, executed and the outputs were verified
successfully.
Ex. No. 1. c) EXPRESSIONS
Date:
Aim:
Program:
#include<stdio.h>
#include<conio.h>
main()
{
float a,b,c,d,x,y,z;
printf("Enter the a value : ");
scanf("%f",&a);
printf("Enter the b value : ");
scanf("%f",&b);
printf("Enter the c value : ");
scanf("%f",&c);
printf("Enter the d value : ");
scanf("%f",&d);
x=(a*b)-c;
printf("\nThe value of x=(a*b)-c : %f\n",x);
y=(b/c)*a;
printf("The value of y=(b/c)*a : %f\n",y);
z=((a-b)/(c+d));
printf("The value of z=((a-b)/(c+d)) : %f",z);
getch();
}
Output:
Program:
#include<stdio.h>
#include<math.h>>
#include<conio.h>
main()
{
int a,b,c,d;
float root1,root2;
printf("Enter the coefficient of x square : ");
scanf("%d",&a);
printf("Enter the coefficient of x : ");
scanf("%d",&b);
printf("Enter the constant value : ");
scanf("%d",&c);
d = b*b - 4*a*c;
root1 = (-b + sqrt(d))/(2*a);
root2 = (-b - sqrt(d))/(2*a);
printf("\n Root 1 Value : %.2f",root1);
printf("\n Root 2 Value : %.2f",root2);
getch();
}
Output:
Result:
Ex. No. 2 DECISION-MAKING CONSTRUCTS
Date:
Aim:
i) Even or Odd
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int number;
printf("Enter an integer: ");
scanf("%d", &number);
if(number % 2 == 0)
printf("\n%d is an Even Number", number);
else
printf("\n%d is an Odd Number", number);
getch();
}
Output:
ii) Greatest among Three numbers
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c;
printf("GREATEST OF THREE NUMBERS");
printf("\n\nEnter the values of A, B and C : ");
scanf("%d%d%d",&a,&b,&c);
if((a>b)&&(a>c))
printf("\nA = %d IS THE GREATEST",a);
else if(b>c)
printf("\nB = %d IS THE GREATEST",b);
else
printf("\nC = %d IS THE GREATEST",c);
getch();
}
Output:
Ex. No. 2. b) DECISION-MAKING CONSTRUCTS – (goto)
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,sum,mark;
float average;
printf("Enter the total number of subjects : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter the marks of subject %d : ",i);
scanf("%d",&mark);
sum=sum+mark;
if(i==n){
goto CALCULATE;
}
}
CALCULATE:
average=sum/n;
printf("\nAverage is :%.3f",average);
getch();
}
Output:
ii) Backward Jump – Multiplication Table
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int n,counter=1,range;
printf("Enter the number for Table: ");
scanf("%d",&n);
printf("\nEnter the range to print: ");
scanf("%d",&range);
TABLE:
while(counter<=range){
printf("%d * %d = %d\n",counter,n,counter*n);
counter++;
goto TABLE;
}
getch();
}
Output:
Ex. No. 2. c) DECISION-MAKING CONSTRUCTS – (switch-case)
i)Arithmetic operators
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,ch;
printf("\nArithmetic Operations: \n\t1.Addition \n\t 2.Subtraction\n\t 3.Multiplication\n\t
4.Division\n\t 5.Modulus\n");
printf("\nEnter the values of a and b : ");
scanf("%d%d",&a,&b);
printf("\nEnter the option : ");
scanf("%d",&ch);
switch(ch){
case 1:
printf("\nSum of a and b is : %d",a+b);
break;
case 2:
printf("\nDifference of a and b is : %d",a-b);
break;
case 3:
printf("\nProduct of a and b is : %d",a*b);
break;
case 4:
printf("\nDivision of a by b is : %d",a/b);
break;
case 5:
printf("\nRemainder of a and b is : %d",a%b);
break;
default:
printf("\nEnter the choice from 1 to 5");
break;
}
getch();
}
Output:
Ex. No. 2. d) DECISION-MAKING CONSTRUCTS – (break-continue)
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,i;
printf("Enter the Number Range: ");
scanf("%d",&a);
printf("Enter the number to break: ");
scanf("%d",&b);
for(i=1;i<=a;i++){
if(i==b){
break;
}else{
printf("%d\n",i);
}
}
getch();
}
Output:
ii)Printing even numbers using continue
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int n,i;
printf("Even Numbers\nEnter the Number : ");
scanf("%d",&n);
for(i=0;i<=n;i++){
if(i%2!=0){
continue;
}else{
printf("%d\n",i);
}
}
getch();
}
Output:
Result:
Ex. No. 3 LOOPS
Date:
Aim:
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int n,sum=0,i;
printf("Enter the number : ");
scanf("%d",&n);
for(i=1;i<=n;i++){
sum=sum+i;
}
printf("\nSum of n numbers is : %d",sum);
getch();
}
Output:
ii)Print star pyramid
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,j,k;
printf("\tSTAR PYRAMID\n\nEnter the number of rows: ");
scanf("%d",&n);
for(i=1;i<=n;i++){
for(j=n-i;j>=1;j--){
printf(" ");
}
for(k=1;k<=i;k++){
printf("* ");
}
printf("\n");
}
getch();
}
Output:
Ex. No. 3. b) LOOPS (while)
i)Reverse of a number
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int n,rev=0,rem;
printf("Enter the number: ");
scanf("%d",&n);
while(n!=0){
rem=n%10;
rev=(rev*10)+rem;
n=n/10;
}
printf("\nReverse of the number is : %d",rev);
getch();
}
Output:
ii)Palindrome number or not
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int n,rev=0,rem,original;
printf("Enter a number : ");
scanf("%d",&n);
original=n;
while(n!=0){
rem=n%10;
rev=(rev*10)+rem;
n=n/10;
}
if(original==rev){
printf("\nThe number is palindrome");
}else{
printf("\nThe number is not palindrome");
}
getch();
}
Output:
Ex. No. 3. c) LOOPS (do-while)
i)Sum of digits of a number
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int n,sum=0,rem;
printf("Enter a number: ");
scanf("%d",&n);
do{
rem=n%10;
sum=sum+rem;
n=n/10;
}while(n!=0);
printf("\nThe Sum of digits of the number is : %d",sum);
getch();
}
Output:
Result:
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,a[10],sum=0;
printf("Enter the number of elements in the array: ");
scanf("%d",&n);
for(i=0;i<=n-1;i++)
{
printf("\nEnter the element %d : ",i+1);
scanf("%d",&a[i]);
}
for(i=0;i<=n-1;i++)
{
sum=sum+a[i];
}
printf("\nSum of the numbers is : %d",sum);
getch();
}
Output:
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,l=0,a[10],s;
printf("Enter the number of elements in the Array : ");
scanf("%d",&n);
printf("\nEnter the elements : ");
for(i=0;i<=n-1;i++)
{
scanf("%d",&a[i]);
}
printf("\nEnter an element to Search : ");
scanf("%d",&s);
for(i=0;i<=n-1;i++)
{
if(a[i]==s)
{
l=1;
break;
}
}
if(l==1)
{
printf("\nThe element is found.");
}
else
printf("The element is not found.");
getch();
}
Output:
Ex. No. 4. a) ii) 1) Two Dimensional Array – Matrix Addition
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int ar,ac,br,bc,cr,cc,i,j,a[10][10],b[10][10];
printf("Enter the number of rows and columns of matrix A :\n");
scanf("%d%d",&ar,&ac);
printf("Enter the number of rows and columns of matrix B :\n");
scanf("%d%d",&br,&bc);
cr=ar,cc=ac;
printf("\n");
for(i=0;i<=ar-1;i++)
{
for(j=0;j<=ac-1;j++)
{
printf("Enter the value of row %d and column %d of matrix A: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
printf("\n");
for(i=0;i<=br-1;i++)
{
for(j=0;j<=bc-1;j++)
{
printf("Enter the value of row %d and column %d of matrix B: ",i+1,j+1);
scanf("%d",&b[i][j]);
}
}
printf("\n\nAddition of Matrix A and B:\n\n");
for(i=0;i<=cr-1;i++){
for(j=0;j<=cc-1;j++){
printf("%d\t",a[i][j]+b[i][j]);
}
printf("\n");
getch();
}
Output:
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int ar,ac,br,bc,cr,cc,i,j,k,a[10][10],b[10][10],c[10][10];
printf("Enter the row and column of A : \n");
scanf("%d%d",&ar,&ac);
printf("Enter the row and column of B : \n");
scanf("%d%d",&br,&bc);
if(ac==br)
{
cr=ar,cc=bc;
printf("\nEnter the values of Matrix A\n\n");
for(i=0;i<=ar-1;i++){
for(j=0;j<=ac-1;j++){
printf("Enter a value %d %d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
printf("\nEnter the values of Matrix B\n\n");
for(i=0;i<=br-1;i++){
for(j=0;j<=bc-1;j++){
printf("Enter a value %d %d: ",i+1,j+1);
scanf("%d",&b[i][j]);
}
}
printf("\n\n The multiplication of Matrix A and B\n\n");
for(i=0;i<=cr-1;i++){
for(j=0;j<=cc-1;j++){
c[i][j]=0;
for(k=0;k<=cr-1;k++){
c[i][j]+=a[i][k]*b[k][j];
}
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
else
{
printf("Row of Matrix A and Column of Matrix B should be same\n");
}
getch(); }
Output:
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int ax,ay,az,bx,by,bz,cx,cy,cz,i,j,k,a[10][10][10],b[10][10][10],c[10][10][10];
printf("Enter the dimensions of A : \n");
scanf("%d%d%d",&ax,&ay,&az);
printf("Enter the dimensions of B : \n");
scanf("%d%d%d",&bx,&by,&bz);
cx=ax,cy=ay,cz=az;
for(i=0;i<=ax-1;i++){
for(j=0;j<=ay-1;j++){
for(k=0;k<=az-1;k++){
printf("Enter a %d%d%d :",i+1,j+1,k+1);
scanf("%d",&a[i][j][k]);
}
}
}
printf("\n");
for(i=0;i<=bx-1;i++){
for(j=0;j<=by-1;j++){
for(k=0;k<=bz-1;k++){
printf("Enter b %d%d%d :",i+1,j+1,k+1);
scanf("%d",&b[i][j][k]);
}
}
}
printf("\nSubtraction of matrix A and B:\n");
for(i=0;i<=cx-1;i++){
for(j=0;j<=cy-1;j++){
for(k=0;k<=cz-1;k++){
printf("%d\t",a[i][j][k]-b[i][j][k]);
}
printf("\n");
}
printf("\n");
getch();
}
Output:
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,j,a[10],temp;
printf("\n\tSelection Sort\n\n");
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("\nEnter the elements:\n");
for(i=0;i<=n-1;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<=n-1;i++) {
for(j=i+1;j<=n-1;j++) {
if(a[j]<a[i]) {
temp=a[i];
a[i]=a[j];
a[j]=temp;
} } }
printf("\n The sorted array is : \n\n");
for(i=0;i<=n-1;i++){
printf("%d ",a[i]);
}
getch();
}
Output:
Result:
Ex. No. 5 STRINGS: OPERATIONS
Date:
Aim:
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char s1[10],s2[10];
int c=1;
printf("Enter a String : ");
scanf("%s",s1);
strcpy(s2,s1); //copy from s1 to s2
strrev(s1); //reversing string : s1
c=strcmp(s1,s2);
if(c==0)
{
printf("The String is Palindrome ");
}
else
printf("The String is not a Palindrome ");
getch();
}
Output:
Algorithm:
Program:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
int main(){
int choice=0;
char s1[10],s2[10];
printf(" String Operations\n\n1.Length of a String\n2.Concatenate\n3.Lower Case\n4.Upper
Case\n5.Exit\n");
while(choice!=5)
{
printf("\n\nEnter the choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter a string: ");
scanf("%s",s1);
printf("The Length of the String : %d",strlen(s1));
break;
case 2:
printf("Enter first string: ");
scanf("%s",s1);
printf("Enter second string: ");
scanf("%s",s2);
strcat(s1,s2);
printf("String after concatenation : %s",s1);
break;
case 3:
printf("Enter a string: ");
scanf("%s",s1);
printf("Lower case of the string : %s",strlwr(s1));
break;
case 4:
printf("Enter a string: ");
scanf("%s",s1);
printf("Upper case of the string : %s",strupr(s1));
break;
case 5:
exit(1);
default:
printf("Enter the choice between 1 to 4 ");
break;
}
}
getch();
Output:
Result:
Ex. No. 6 FUNCTIONS
Date:
Aim:
Algorithm:
Program:
#include <stdio.h>
#include <conio.h>
main()
{
int add(int,int); //Function Declaration
int a,b,c;
printf("\tFunction with Argument and Return Value");
printf("\n\nEnter two numbers: ");
scanf("%d %d",&a,&b);
c=add(a,b); //Function Call
printf("\nThe result is %d",c);
getch();
}
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
int swap(int x,int y);
main()
{
int a,b;
printf("\n\tPass by Value\n");
printf("\nEnter two Numbers: ");
scanf("%d%d",&a,&b);
printf("\nBefore Swapping : a=%d b=%d",a,b);
swap(a,b);
printf("\nAfter Swapping :\n \t\t In Main() : a=%d b=%d",a,b);
getch();
}
int swap(int x,int y)
{
int t;
t=x;
x=y;
y=t;
printf("\n\nAfter Swapping :\n \t\t In swap() : x=%d y=%d",x,y);
}
Output:
Program:
#include<stdio.h>
#include<conio.h>
int swap(int*,int*);
main()
{
int a,b;
printf("\n\tPass by Reference\n");
printf("\nEnter two Numbers: ");
scanf("%d%d",&a,&b);
printf("\nBefore Swapping : a=%d b=%d",a,b);
swap(&a,&b);
printf("\nAfter Swapping :\n \t\t In Main() : a=%d b=%d",a,b);
getch();
}
int swap(int* x,int* y)
{
int t;
t=*x;
*x=*y;
*y=t;
printf("\n\nAfter Swapping :\n \t\t In swap() : x=%d y=%d",*x,*y);
}
Output:
Algorithm:
Program:
#include <stdio.h>
#include <conio.h>
int main()
{
int n,m,a[100],i;
int max(int*,int);
printf("\n\tPassing Arrays to Function\n");
printf("\nEnter the Number of Elements in the Array : ");
scanf("%d",&n);
printf("\nEnter the array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
m=max(a,n);
printf("\nMaximum element in an Array is %d\n",m);
getch();
}
int max(int* arr,intnum)
{
int max_value, i;
max_value = arr[0];
for(i = 1; i<num; i++)
if(arr[i] >max_value)
max_value = arr[i];
return max_value;
}
Output:
Result:
Ex. No. 7 RECURSION
Date:
Aim:
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
long int fact(int);
main()
{
int n;
long int f;
printf("\tRECURSION: FUNCTION\n\n");
printf("Enter the number: ");
scanf("%d",&n);
f=fact(n);
printf("\nFactorial of %d is %ld",n, f);
getch();
}
Result:
Ex. No. 8 POINTERS
Date:
Aim:
Array of Pointers
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
main( )
{ int a=10,b=20,c=30;
int *a1[]={&a,&b,&c};
printf("\nElements are:");
printf("\n%d %d %d",a,b,c);
printf("\n%d %d %d",*a1[0],*a1[1],*a1[2]);
getch();
}
Output:
Result:
Ex. No. 9 STRUCTURES AND UNION
Date:
Aim:
Pointers to Structures
Algorithm:
Program:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct emp
{
int empno ;
char name[10] ;
int bpay, HRA, DA, PF, IT, npay ;
};
main()
{
int i, n, allow,ded ;
struct emp *e;
printf("Enter the number of employees : ") ;
scanf("%d", &n) ;
e = (struct emp*)malloc(sizeof(struct emp) * n);
for(i = 0 ; i< n ; i++)
{
printf("\nEnter the employee number %d: ",i+1) ;
scanf("%d", &e[i].empno) ;
printf("\nEnter the name : ") ;
scanf("%s", e[i].name) ;
printf("\nEnter the basic pay:") ;
scanf("%d", &e[i].bpay) ;
e[i].DA = e[i].bpay*0.2;
e[i].HRA = e[i].bpay*0.1;
e[i].PF = e[i].bpay*0.05;
e[i].IT = e[i].bpay*0.01;
allow=e[i].DA+e[i].HRA;
ded=e[i].PF+e[i].IT;
e[i].npay = e[i].bpay + allow - ded ;
}
printf("\nEmp. No. Name \t Bpay \t Npay \n\n") ;
for(i = 0 ; i< n ; i++)
{
printf(" %d\t %s\t %d\t %d \n", e[i].empno,e[i].name, e[i].bpay, e[i].npay) ;
}
getch();
}
Output:
Union
Algorithm:
Program:
union employee
{
char name[10];
int idno;
float salary;
}e;
main()
{
printf(“Enter the name\n”);
scanf(“%s”,e.name);
printf(“Enter the id number\n”);
scanf(“%d”,&e.idno);
printf(“Enter the salary\n”);
scanf(“%f”,&e.salary);
printf(“Name : %s\n”,e.name);
printf(“Id number : %d\n”,e.idno);
printf(“Salary : %f\n”,e.salary);
getch();
return;
}
Output :
Result:
Ex. No. 10 FILES
Date:
Aim:
Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<conio.h>
typedef struct Phonebook_Contacts
{
char FirstName[20];
char LastName[20];
char PhoneNumber[20];
} phone;
void AddEntry(phone * ); void DeleteEntry(phone * );
void PrintEntry(phone * );
void SearchForNumber(phone * );
int counter = 0;
char FileName[256];
FILE *pRead;
FILE *pWrite;
int main (void)
{
phone *phonebook;
phonebook = (phone*) malloc(sizeof(phone)*100);
int iSelection = 0;
if (phonebook == NULL)
{
printf("Out of Memory. The program will now exit");
return 1;
}
else {}
do
{
printf("\n\t\t\tPhonebook Menu");
printf("\n\n\t(1)\tAdd Friend");
printf("\n\t(2)\tDelete Friend");
printf("\n\t(3)\tDisplay Phonebook Entries");
printf("\n\t(4)\tSearch for Phone Number");
printf("\n\t(5)\tExit Phonebook");
printf("\n\nWhat would you like to do? ");
scanf("%d", &iSelection);
if (iSelection == 1)
{
AddEntry(phonebook);
}
if (iSelection == 2)
{
DeleteEntry(phonebook);
}
if (iSelection == 3)
{ PrintEntry(phonebook);
}
if (iSelection == 4)
{
SearchForNumber(phonebook);
}
if (iSelection == 5)
{
printf("\nYou have chosen to exit the Phonebook.\n");
getch();
}
} while (iSelection<= 4);
}
void AddEntry (phone * phonebook)
{
pWrite = fopen("phonebook_contacts.dat", "a");
if ( pWrite == NULL )
{
perror("The following error occurred ");
exit(EXIT_FAILURE);
}
else
{
counter++;
realloc(phonebook, sizeof(phone));
printf("\nFirst Name: ");
scanf("%s", phonebook[counter-1].FirstName);
printf("Last Name: ");
scanf("%s", phonebook[counter-1].LastName);
printf("Phone Number (XXX-XXX-XXXX): ");
scanf("%s", phonebook[counter-1].PhoneNumber);
printf("\n\tFriend successfully added to Phonebook\n");
fprintf(pWrite, "%s\t%s\t%s\n", phonebook[counter-1].FirstName,
phonebook[counter-1].LastName, phonebook[counter-1].PhoneNumber);
fclose(pWrite);
}
}
void DeleteEntry (phone * phonebook)
{
int x = 0;
int i = 0;
char deleteFirstName[20]; //
char deleteLastName[20];
printf("\nFirst name: ");
scanf("%s", deleteFirstName);
printf("Last name: ");
scanf("%s", deleteLastName);
for (x = 0; x < counter; x++)
{
if (strcmp(deleteFirstName, phonebook[x].FirstName) == 0)
{
if (strcmp(deleteLastName, phonebook[x].LastName) == 0)
{
for ( i = x; i< counter - 1; i++ )
{
strcpy(phonebook[i].FirstName, phonebook[i+1].FirstName);
strcpy(phonebook[i].LastName, phonebook[i+1].LastName);
strcpy(phonebook[i].PhoneNumber, phonebook[i+1].PhoneNumber);
}
printf("Record deleted from the phonebook.\n\n");
--counter;
return;
}
}
}
printf("That contact was not found, please try again.");
}
void PrintEntry (phone * phonebook)
{
int x = 0;
printf("\nPhonebook Entries:\n\n ");
pRead = fopen("phonebook_contacts.dat", "r");
if ( pRead == NULL)
{
perror("The following error occurred: ");
exit(EXIT_FAILURE);
}
else
{
for( x = 0; x < counter; x++)
{
printf("\n(%d)\n", x+1);
printf("Name: %s %s\n", phonebook[x].FirstName, phonebook[x].LastName);
printf("Number: %s\n", phonebook[x].PhoneNumber);
}
}
fclose(pRead);
}
void SearchForNumber (phone * phonebook)
{
int x = 0;
char TempFirstName[20];
char TempLastName[20];
printf("\nPlease type the name of the friend you wish to find a number for.");
printf("\n\nFirst Name: ");
scanf("%s", TempFirstName);
printf("Last Name: ");
scanf("%s", TempLastName);
for (x = 0; x < counter; x++)
{
if (strcmp(TempFirstName, phonebook[x].FirstName) == 0)
{
if (strcmp(TempLastName, phonebook[x].LastName) == 0)
{
printf("\n%s %s's phone number is %s\n", phonebook[x].FirstName,
phonebook[x].LastName, phonebook[x].PhoneNumber);
}
}
}
}
Output:
Result: