210246: PROGRAMMING LABORATORY
Teaching Scheme Examination Scheme :
Practical
:
4 Hours / Week T
erm Work
:
25 Marks
Practical :
50 Marks
Suggested List of Assignments Based on Data Structures and
Algorithm:
1. Write a program to perform Set operations - Union,
Intersection, Difference, Symmetric Difference etc.
2. Write a program to perform various string operations such as
Copy, Length, Reversing, Palindrome, Concatenation and to find
occurrence substring etc with and without using library
functions.
3. Write a program to perform operations on matrices like
addition, multiplication, saddle point, magic square ,inverse &
transpose etc using functions & pointers.[Minimum 4 operations]
4. Write a program to perform following operations on any
database: Add, Delete, Modify, Display, Search & Sort etc.
5. Implement Sorting Methods using functions- Bubble Sort,
Selection Sort, Insertion Sort, and Shell Sort.
6. Implement Sorting Methods using recursion- Quick Sort and
Merge Sort.
7. Implement Searching Methods-Sequential Search, Binary Search,
Fibonacci Search and Index Sequential Search.[Minimum 3
searching methods]
8. Represent polynomial using structures and write a menu driven
program to perform Addition, Multiplication and Evaluation.
9. Represent Sparse Matrix using array and perform Matrix
Addition, Simple and Fast Transpose.
10.
Write a menu driven program to perform following
operations on SLL/CDLL : Create, Insert – Start, end, between,
Search & delete, Reverse ,Display etc.
two lists into one list without creating a new node or swapping
of the data.
12.
Represent a polynomial using Circular Linked List and
write a menu driven program to perform Addition, Multiplication
and Evaluation.
13.
Implement Stack as an ADT using Array. Use this ADT to
perform expression conversion and evaluation (infix to postfix,
infix to prefix, prefix to infix, prefix to postfix, postfix to
infix and postfix to prefix).
14.
Represent Circular Queue using Linked List and write a
program to perform operations like Insert, Delete, Finding
front and rear element.
15.
Implement the Mini Project of Student Database using
Linked list for following requirements:
•
Creation of Student Database in memory containing student ID,
Name, Name Initials, Address, Contact No and Date of Birth .
•
Insertion, Deletion, Modification of student record for a
given student ID.
•
Sorting on name initials and searching a particular student
record on name initials
Note: All Assignments to be implemented using C and Time and
Space Complexity is to be verified with theoretical findings.
•
Students will submit Term Work in the form Journal that will
include minimum above 15 assignments. Each assignment will
consist of Pseudo algorithm, program listing with proper
documentation and printout of the output.
Practical – 1.
Problem Definition :
Write a menu driven program in 'C' language
to find union ,intersection ,Difference and Symmetric difference
of two sets.
Objectives :
1. to represent sets.
2. to know how to write operations on set.
3. to write menu driven code in c language.
4. Use array properly.
5. Improve logic.
Theory:
The following operations are possible on sets. These
operations in details are given below.
Union:
Union of two sets means a set of all elements from both sets
but just take common elements only once.
For Example: A = { 1,2,3,4} B = {2,5,7,9}
A U B = { 1,2,3,4,5,7,9}
Intersection:
The INTERSECTION of two sets is the set of elements which are
in both sets.
For example: let A = (1,2,3) and B = (3,4,5). The
INTERSECTION of A and B, written A B = (3).
Sometimes there will be no intersection at all. In that case
we say the answer is the "empty set" or the "null
Difference
:
Difference of set A-B is set of all elements of set A which
are not present in Set B.
For Example:
Symmetric Difference:
Symmetric difference of A and B is A & B = (A – B) U (B - A)
Example:
A = {1,2,3,4,5} and B = {2,4,6,8}, then find symmetric difference
of A and B.
Solution:
A - B = {1,2,3,4,5} – {2,4,6,8} = {1,3,5}
B - A = {2,4,6,8} – {1,2,3,4,5} = {6,8}
(A - B) U (B - A) = {1,3,5} U {8} = {1,3,5,6,8}
The Symmetric difference of A and B is {1,3,5,6,8}
Complement :
In
,s
et theory a
complement
of a set
A
refers to things not in
(that is, things outside of),
A
.
Relative Complement:
The
relative complement
of
A
with respect to a set
B
, is the
set of elements in
B
but not in
A
.
For Example: A = {1,2,3,4,5} B = {4,5,6,7,8,9}
Relative complement of set A w.r.t. B = { 6,7,8,9}
Absolute Complement:
When all sets under consideration are considered to be
subsets of a given set
U
, the
absolute complement
of
A
is the set
of all elements in
U
but not in
A
.
U = { 1,2,3,4,5,6,7,8,9}
A = {1,2,3,4,5} B = {4,5,6,7,8,9}
/*---Program : Write a program to perform Set operations - Union, Intersection, Difference, Symmetric Difference etc.
---*/
#include<stdio.h>
int create(int x[100],int n) //create set of n elements
{
int i;
printf("\nEnter elements of set\n");
for(i=0;i<n;i++)
{
scanf("%d",&x[i]);
}
return(0);
}
//int create(int x{100],int n);
//protypes
int uni (int x[100],int y[100],int n,int m);
int inter (int x[100],int y[100],int n, int m);
int diff(int x[100],int y[100],int n, int m);
int diff2(int x[100],int y[100],int n, int m);
int sydiff(int x[100],int y[100],int n, int m);
int main()
{
int x[100],y[100],z[200];
int ch,m,n,i,j,c;
do
{
printf("---Welcome---\n");
printf("1.union \n");
printf("2.intersection \n");
printf("3.differance(y-x) \n");
printf("4.differance(x-y) \n");
printf("5.symmetric differance \n");
printf("6.EXIT \n");
printf(".Enter ur choice: \n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter size of X & y set ");
scanf("%d%d",&n,&m);
printf("\nFirst set");
create(x,n);
printf("\nSecond set");
create(y,m);
uni(x,y,n,m); //union function is called
break;
case 2:
printf("\nEnter size of X & y set ");
scanf("%d%d",&n,&m);
printf("\n\nFirst set");
create(x,n);
printf("\n\nSecond set");
inter(x,y,n,m);// intersection function is called
break;
case 3:
printf("\nEnter size of X & y set");
scanf("%d%d",&n,&m);
printf("\n\nFirst set");
create(x,n);
printf("\n\nSecond set");
create(y,m);
diff(x,y,n,m); //differance
function is called
break;
case 4:
printf("\nEnter size of X & y set ");
scanf("%d%d",&n,&m);
printf("\n\nFirst set");
create(x,n);
printf("\n\nSecond set");
create(y,m);
diff2(x,y,n,m); //differance
function is called
break;
case 5:
printf("\nEnter size of X & y set ");
printf("\n\nFirst set");
create(x,n);
printf("\n\nSecond set");
create(y,m);
sydiff(x,y,n,m); // symmetric diffence function is called
break;
}
printf("\n Do you want to continue(0/1):");
scanf("%d",&c);
}while(c==1);
}
// union of two sets
int uni (int x[100],int y[100],int n,int m)
{
int i,j,cnt=0,k=0;
int z[200];
for(i=0;i<n;i++)
{
z[k]=x[i]; //copy elements of set x into set z
k++;
for(j=0;j<m;j++)
{
for(i=0;i<n;i++)
{
if(y[j]==x[i]) //check for common elements if yes then donot copy them
{
cnt++;
}
}
if(cnt==0)
{
z[k]=y[j]; //copy elements of set y into set z
k++;
}
cnt=0;
}
printf("Elements after union are :\n");
for(i=0;i<k;i++)
{
printf("%d\n",z[i]);
}
return 0;
}
int inter (int x[100],int y[100],int n, int m)
{
int i,j,k=0;
int z[100];
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(x[i]==y[j])
{
z[k]=x[i]; //copy common elements of x & y in set z
k++;
break;
}
}
}
if(k==0)
printf("sorry no intrsection");
else
printf("Intersected elements are :");
for(i=0;i<k;i++)
{
printf("%d\n",z[i]);
}
return 0;
}
int diff(int x[100],int y[100],int n, int m)
{
int i,j,k=0,cnt=0;
int z[100];
for(j=0;j<m;j++)
{
for(i=0;i<n;i++)
{
if(x[i]==y[j])
{
cnt++;
}
}
if(cnt==0)
{
z[k]=y[j]; //copy elments of y which are not present in x in set z
k++;
}
cnt=0;
}
printf("diff elements are(y-x):");
for(i=0;i<k;i++)
{
printf("%d\n",z[i]);
return 0;
}
// differance of two sets (x-y)
int diff2(int x[100],int y[100],int n, int m)
{
int i,j,k=0,cnt=0;
int w[100];
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(x[i]==y[j])
{
cnt++;
}
}
if(cnt==0)
{
w[k]=x[i]; //copy elments of set x which are not present in set y in set z
k++;
}
cnt=0;
}
for(i=0;i<k;i++)
{
printf("%d\n",w[i]);
}
return 0;
}
// symmetric differnce of two sets
int sydiff(int x[100],int y[100],int n, int m)
{
int i,j,k=0,cnt=0,l,p=n,q=m;
int z[100],w[100];
for(j=0;j<m;j++)
{
for(i=0;i<n;i++)
{
if(x[i]==y[j])
{
cnt++;
}
}
if(cnt==0)
{
z[k]=y[j]; //copy elments of y which are not present in x in set z
k++;
cnt=0;
}
l=0;
cnt=0;
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
if(x[i]==y[j])
{
cnt++;
}
}
if(cnt==0)
{
z[k]=x[i]; //copy elments of x which are not present in y in set z
k++;
}
cnt=0;
}
printf("\n symmetric difference :");
for(i=0;i<k;i++)
{
printf("%d\n",z[i]); //print result
return 0;
}
/*
OUTPUT :
---menu---
---Welcome---1.union
2.intersection
3.differance(y-x)
4.differance(x-y)
5.symmetric differance
6.EXIT
.Enter ur choice:
1
Enter size of X & y set :
3
4
First set
Enter elements of set :
1
2
3
Second set
Enter elements of set :
2
5
6
Elements after union are :
1
2
3
4
5
6
Do you want to continue(0/1):1
---menu---
---Welcome---1.union
2.intersection
3.differance(y-x)
4.differance(x-y)
5.symmetric differance
6.EXIT
.Enter ur choice:
2
Enter size of X & y set :
4
5
First set
Enter elements of set:
2
3
4
Second set
Enter elements of set:
2
6
9
8
5
Intersected elements are :
2
Do you want to continue(0/1):1
---menu---
---Welcome---1.union
2.intersection
3.differance(y-x)
4.differance(x-y)
5.symmetric differance
6.EXIT
.Enter ur choice:
3
Enter size of X & y set:
4
First set
Enter elements of set:
2
3
4
5
Second set
Enter elements of set
9
8
64
4
diff elements are(y-x):
9
8
64
Do you want to continue(0/1):1
---menu---
---Welcome---1.union
2.intersection
3.differance(y-x)
4.differance(x-y)
6.EXIT
.Enter ur choice:
4
Enter size of X & y set:
3
3
First set
Enter elements of set:
1
2
3
Second set
Enter elements of set:
3
5
6
diff elements are(x-y):1
2
Do you want to continue(0/1):1
---menu---
---Welcome---1.union
2.intersection
4.differance(x-y)
5.symmetric differance
6.EXIT
.Enter ur choice:
5
Enter size of X & y set:
5
6
First set
Enter elements of set:
1
2
3
4
5
Second set
Enter elements of set:
4
5
6
7
8
9
6
7
8
9
1
2
3
Do you want to continue(0/1):0
Practical 2.
Problem Definition:
Write a program to perform various string
operations such as Copy, Length, Reversing, Palindrome,
Concatenation and to find occurrence sub string etc. with and
without using library functions.
Objectives :
1. to know how to represent string in c language
2. to know available string library
Theory:
'C'
language provides number of string library functions
which are included in string.h header file. The syntax and
explaination details are given below:
char *strcpy( char *s1, const char *s2)
copies the string
s2
into the character array
s1
.The value of
s1
is returned.
char *strncpy( char *s1, const char *s2, size_t n)
copies at most
n
characters of the string
s2
into the character
array
s1
. The value of
s1
is returned.
char *strcat( char *s1, const char *s2)
appends the string
s2
to the end of character array
s1
.The
first character from
s2
overwrites the '\0' of
s1
. The value
of
s1
is returned.
char *strncat( char *s1, const char *s2, size_t n)
appends at most
n
characters of the string
s2
to the end of
character array
s1
. The first character from
s2
overwrites
the '\0' of
s1
. The value of
s1
is returned.
char *strchr( const char *s, int c)
returns a pointer to the first instance of
c
in s. Returns a
NULL
pointer if
c
is not encountered in the string.
char *strrchr( const char *s,int c)
returns a pointer to the last instance of
c
in s. Returns a
NULL
pointer if
c
is not encountered in the string.
compares the string
s1
to the string
s2
. The function returns 0
if they are the same, a number < 0 if
s1
<
s2
, a number > 0
if
s1
>
s2
.
int strncmp( const char *s1, const char *s2, size_t n)
compares up to
n
characters of the string
s1
to the string
s2
.
The function returns
0
if they are the same, a number < 0 if
s1
<
s2
, a number > 0 if
s1
>
s2
.
size_t strspn( char *s1, const char *s2)
returns the length of the longest substring of
s1
that begins
at the start of
s1
and consists
only
of the characters found
in
s2
.
size_t strcspn( char *s1, const char *s2)
returns the length of the longest substring of
s1
that begins
at the start of
s1
and contains
none
of the characters found
in
s2
.
size_t strlen( const char *s)
determines the length of the string
s
. Returns the number of
characters in the string
before
the '\0'.
char *strpbrk( const char *s1, const char *s2)
returns a pointer to the first instance in
s1
of any character
found in
s2
. Returns a
NULL
pointer if no characters from
s2
are encountered in
s1
.
char *strstr( const char *s1, const char *s2)
returns a pointer to the first instance of string
s2
in
s1
.
Returns a
NULL
pointer if
s2
is not encountered in
s1
.
char *strtok(char *s1, const char *s2)
Program:
1) Write a program to perform various string operations such as Copy, Length, Reversing, Palindrome, Concatenation and to find occurrence sub string etc with and without using library functions.
#include<stdio.h>
#include<string.h>
int main()
{
int x,ch,c;
char str1[20],str2[20],z[20],f;
char *ptr;
do
{
printf("\t\t---MEnu---");
printf("\n\n\t1.Find string lenth:");
printf("\n\n\t2.To Copy the string");
printf("\n\n\t3.To reverse the string:");
printf("\n\n\t4.To compare the string:");
printf("\n\n\t5.To concanate the string:");
printf("\n\n\t6.To Find occurance of character in perticuler string: ");
printf("\n\n\t7.To find substring in given string:");
printf("\n\n\t8.Convert upper case to lower case:");
printf("\n\n\t9.Convert lower case to upper case:");
printf("\n\n\t10.palindrome the string:");
printf("\n\n\t11. Exit");
printf("\n\n\tEnter your choice:");
scanf("%d",&ch);
switch(ch)
case 1:
printf("\n\n\tEnter a String:");
gets(str1);
printf("\n\n\tString is %s",str1);
x=strlen(str1); //library fun to find lengh
printf("\n\n\t Length of string :%d",x);
break;
case 2:
printf("\n\n\tEnter a source String:");
gets(str1);
printf("\n\n\tString is %s",str1);
strcpy(str2,str1); //copy string1 into string2
printf("\n\n\tcopied string is: %s",str2);
break;
case 3:
printf("\n\n\tEnter a String to reverse:");
gets(str1);
printf("\n\n\tString is %s",str1);
strrev(str1); //reverses string1
printf("\n\n\t the reverse string is :%s",str1);
break;
case 4:
printf("\n\n\tEnter a String 1:");
gets(str1);
printf("\n\n\tString is %s",str1);
printf("\n\n\tEnter a String 2:");
gets(str2);
printf("\n\n\tString is %s",str2);
x= strcmp(str1,str2); //compares two strings by
if(x==0)
{
printf("\n\n\tboth the strings are equal");
}
else if(x>0)
{
printf("\n\tString1 is greter..");
}
else if(x<0)
{
printf("\n\n\tString2 is greter..");
}
break;
case 5:
printf("\n\n\tEnter a String 1:");
gets(str1);
printf("\n\n\tString is %s",str1);
printf("\n\n\tEnter a String 2:");
gets(str2);
printf("\n\n\tString is %s",str2);
strcat(str1,str2); //append string2 to end of
string1
printf("\n\n\t The concanate string is :%s",str1);
break;
case 6:
printf("\n\n\tEnter a String 1:");
gets(str1);
printf("\n\n\tString is %s",str1);
fflush(stdin); //its clears buffer
scanf("%c",&f);
printf("\n\n\t Character is :%c",f);
ptr=strchr(str1,f); //find first occuerance of
character in string
if(ptr==NULL)
{
printf("\n\n\t Character is not present..");
}
else
{
printf("\n\n\t Charecter is present..");
}
break;
case 7:
printf("\n\n\tEnter a String 1:");
gets(str1);
printf("\n\n\tString is %s",str1);
printf("\n\n\t Enter string to find : ");
gets(str2);
ptr=strstr(str1,str2); //find first occuerance of
substring in string
if(ptr==NULL)
{
printf("\n\n\t string is not present..");
}
else
{
}
break;
case 8:
printf("\n\n\tEnter a String 1:");
gets(str1);
printf("\n\n\tString is %s",str1);
strlwr(str1); //convert uppercase to lowercase
printf("\n\n\tlower case of stringis :%s ",str1);
break;
case 9:
printf("\n\n\tEnter a String 1:");
gets(str1);
printf("\n\n\tString is %s",str1);
strupr(str1); //convert lowercase to uppercase
printf("\n\n\tUpper case of string is :%s ",str1);
break;
case 10:
printf("\n\n\tEnter a String 1:");
gets(str1);
printf("\n\n\tString is %s",str1);
strcpy(str2,str1);
strrev(str2);
x=strcmp(str1,str2);
if(x==0) //if both strings are same then
{
printf("\n\n\tThe string is palindrome ");
}
else
{
}
break;
case 11:
exit(0);
}
printf("\n\n\t Do you want to continue(0\1)");
scanf("%d",&c);
}while(c==1);
return(0);
}
/*
OUTPUT :
----MEnu---1.Find string lenth:
2.To Copy the string
3.To reverse the string:
4.To compare the string:
5.To concanate the string:
6.To Find occurance of character in perticuler string:
7.To find substring in given string:
9.Convert lower case to upper case:
10.palindrome the string:
11. Exit
Enter your choice:1
Enter a String:sandipfoundation
String is : sandipfoundation
Lenth of string is : 16
Do you want to continue(0/1) :1
----MEnu---1.Find string lenth:
2.To Copy the string
3.To reverse the string:
4.To compare the string:
6.To Find occurance of character in perticuler string:
7.To find substring in given string:
8.Convert upper case to lower case:
9.Convert lower case to upper case:
10.palindrome the string:
11. Exit
Enter your choice:2
enter source string: computer
target string is: computer
Do you want to continue(0/1) :1
----MEnu---1.Find string lenth:
2.To Copy the string
3.To reverse the string:
5.To concanate the string:
6.To Find occurance of character in perticuler string:
7.To find substring in given string:
8.Convert upper case to lower case:
9.Convert lower case to upper case:
10.palindrome the string:
11. Exit
Enter your choice:3
Enter string :computer
Reverse string is: retupmoc
Do you want to continue(0/1) :1
----MEnu---1.Find string lenth:
2.To Copy the string
4.To compare the string:
5.To concanate the string:
6.To Find occurance of character in perticuler string:
7.To find substring in given string:
8.Convert upper case to lower case:
9.Convert lower case to upper case:
10.palindrome the string:
11. Exit
Enter your choice:4
Enter first string: abc
Enter second string: ABC
string 1 is greater
Do you want to continue(0/1) :1
----MEnu---1.Find string lenth:
3.To reverse the string:
4.To compare the string:
5.To concanate the string:
6.To Find occurance of character in perticuler string:
7.To find substring in given string:
8.Convert upper case to lower case:
9.Convert lower case to upper case:
10.palindrome the string:
11. Exit
Enter your choice:5
Enter the first string :sandip
Enter the string string :foundation
Do you want to continue(0/1) :1
----MEnu---1.Find string lenth:
2.To Copy the string
3.To reverse the string:
4.To compare the string:
5.To concanate the string:
6.To Find occurance of character in perticuler string:
7.To find substring in given string:
8.Convert upper case to lower case:
9.Convert lower case to upper case:
10.palindrome the string:
11. Exit
Enter your choice:6
Enter the charcter to find : n
charcter found
Do you want to continue(0/1) :1
----MEnu---1.Find string lenth:
2.To Copy the string
3.To reverse the string:
4.To compare the string:
5.To concanate the string:
6.To Find occurance of character in perticuler string:
7.To find substring in given string:
8.Convert upper case to lower case:
9.Convert lower case to upper case:
10.palindrome the string:
Enter your choice:7
Enter the first string :computer
Enter sub string to find :ter
substring found
Do you want to continue(0/1) :1
----MEnu---1.Find string lenth:
2.To Copy the string
3.To reverse the string:
4.To compare the string:
5.To concanate the string:
6.To Find occurance of character in perticuler string:
7.To find substring in given string:
8.Convert upper case to lower case:
10.palindrome the string:
11. Exit
Enter your choice:8
Enter the string to convert lowercase:COMPUTER
strings after lowercase is :computer
Do you want to continue(0/1) :1
----MEnu---1.Find string lenth:
2.To Copy the string
3.To reverse the string:
4.To compare the string:
5.To concanate the string:
6.To Find occurance of character in perticuler string:
8.Convert upper case to lower case:
9.Convert lower case to upper case:
10.palindrome the string:
11. Exit
Enter your choice:9
Enter the string to convert uppercase:computer
strings after uppercase is :COMPUTER
Do you want to continue(0/1) :1
----MEnu---1.Find string lenth:
2.To Copy the string
3.To reverse the string:
4.To compare the string:
5.To concanate the string:
7.To find substring in given string:
8.Convert upper case to lower case:
9.Convert lower case to upper case:
10.palindrome the string:
11. Exit
Enter your choice:10
Enter a string :madam
string is :madam
string is palindrome
Do you want to continue(0/1) :0
2)
Write a program to perform various string operations such as Copy, Length, Reversing, Palindrome, Concatenation and to find occurrence sub string etc with and without using library functions.#include<stdio.h>
#include<string.h>
//prototypes
int lengh(char *);
int copy(char *,char *);
int reverse(char *);
int compare(char *,char *);
int concatenate(char *,char*);
int occurance(char *,char);
int substring(char *,char *);
int lowercase(char *);
int uppercase(char *);
int palindrome(char *);
int main()
{
int x,ch,c ;
char str1[20],str2[20],v;
char *ptr;
do
{
printf("\t\t----MEnu---");
printf("\n\n\t1.Find string lenth:");
printf("\n\n\t2.To Copy the string");
printf("\n\n\t3.To reverse the string:");
printf("\n\n\t4.To compare the string:");
printf("\n\n\t5.To concanate the string:");
printf("\n\n\t7.To find substring in given string:");
printf("\n\n\t8.Convert upper case to lower case:");
printf("\n\n\t9.Convert lower case to upper case:");
printf("\n\n\t10.palindrome the string:");
printf("\n\n\t11. Exit");
printf("\n\n\tEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n\n\tEnter a String:");
scanf("%s",&str1);
printf("\n\n\t String is : %s ",str1);
x=length(str1); //length function is called
printf("\n\n\t Lenth of string is : %d",x);
break;
case 2:
printf("\n\n enter source string: ");
scanf("%s",str1);
copy(str1,str2); //copy fun is called
printf("\n\n target string is: %s ",str2);
break;
case 3:
printf("\n\t Enter string :");
scanf(" %s",&str1);
reverse(str1); //reverse fun is called
printf("\n Reverse string is: %s",str1);
break;
case 4:
scanf("%s",str1);
printf("\n Enter second string ");
scanf("%s",str2);
x=compare(str1,str2); //compare fun is called
if(x==0)
printf("\n Both strings are equal ");
else
if(x>0)
printf("\n string 1 is greater ");
else
printf("\n string 2 is greater ");
break;
case 5:
printf("\n\n\t Enter the first string :");
scanf("%s",str1);
printf("\n\n\t Enter the string string :");
scanf("%s",str2);
concatenate(str1,str2); //concatination fun is called
printf(" \n\n\t strings after concatenate is :%s",str1);
break;
case 6 :
printf("\n\n\t Enter the first string :");
scanf("%s",str1);
printf("\n\n\t Enter the charcter to find :");
fflush(stdin);
scanf("%c",&v);
occurance(str1,v); //occurence fun is called
break;
printf("\n\n\t Enter the first string :");
scanf("%s",str1);
printf("\n\n\t Enter sub string to find :");
scanf("%s",str2);
substring(str1,str2); //substring fun is called
break;
case 8 :
printf("\n\n\t Enter the string to convert lowercase:");
scanf("%s",str1);
lowercase(str1); //lowercase fun is called
printf(" \n\n\t strings after lowercase is :%s",str1);
break;
case 9 :
printf("\n\n\t Enter the string to convert uppercase:");
scanf("%s",str1);
uppercase(str1); //uppercse fun is called
printf(" \n\n\t strings after uppercase is :%s",str1);
break;
case 10 :
printf("\n\n Enter a string :");
scanf("%s",&str1);
printf("\n\n string is :%s",str1);
palindrome(str1); //palindrome fun is called
break;
case 11 :
}
printf("\n\nDo you want to continue(0/1) :");
scanf("%d",&c);
}while(c==1);
return(0);
}
int length(char *s) //length function
{
int i,len=0;
while(*s!='\0')
{
len++; //length calculated
s++;
}
return len;
}
int copy(char *str1,char *str2) //copy funtion
{
while(*str1!='\0')
{
*str2=*str1; //copy process
str1++;
str2++;
}
*str2='\0';
return(0);
}
int reverse(char *str1) //reverse fun
char *ptr,temp;
int len,i,j;
len=length(str1);
j=len-1;
ptr=&str1[0];
for(i=0;i<j;i++)
{
temp=*(ptr+i); //reverse process
*(ptr+i)=*(ptr+j);
*(ptr+j)=temp;
j--;
}
return(0);
}
int compare(char *str1,char *str2) //compare function
{
while(*str1!='\0')
{
if(*str1 > *str2)
return(1); //process of comparision
if(*str1 < *str2)
return(-1);
str1++;
str2++;
}
return(0);
}
{
while(*str1!='\0')
{
str1++;
}
while(*str2!='\0')
{
*str1=*str2; //process
str1++;
str2++;
}
*str1='\0';
return(0);
}
int palindrome(char *str1) //palindrome function
{
char str2[20];
int x;
copy(str1,str2);
reverse(str2);
x=compare(str1,str2);
if(x==0)
printf("\n\n string is palindrome ");
else
printf("\n\n string is not palindrome ");
return(0);
}
int occurance(char *str1,char b) //to find occurance of char
char *ptr;
int cnt;
for(ptr=&str1[0];*ptr!='\0';ptr++)
{
if(*ptr==b)
{
cnt=1;
break;
}
}
if(cnt==1)
{
printf("\n\n\t character found");
}
else
printf("\n\n\t charcter not found");
return(0);
}
int substring(char *str1,char *str2) //to find substring
{
char *ptr1;
char *ptr2;
int cnt,cha;
cnt=length(str2);
for(ptr1=&str1[0];*ptr1!='\0';ptr1++)
{
for(ptr2=&str2[0];*ptr2!='\0';ptr2++)
{
{
ptr1++;
cha=1;
if(cnt<=0)
{
break;
}
cnt--;
}
else
{
break;
}
}
}
if(cha==1)
printf("\n substring found ");
else
printf("\n substring not found ");
return(0);
}
int lowercase(char *str1) //convert uppercase to lowercase
{
while(*str1!='\0')
{
if(*str1>=65&&*str1<=90)
*str1=*str1+32;
}
str1++;
}
return(0);
}
int uppercase(char *str1) //convert lowercase to uppercase
{
while(*str1!='\0')
{
if(*str1>=95&&*str1<=122)
{
*str1=*str1-32;
}
str1++;
}
return(0);
}
/*
OUTPUT :
----MEnu---1.Find string lenth:
2.To Copy the string
3.To reverse the string:
4.To compare the string:
5.To concanate the string:
6.To Find occurance of character in perticuler string:
7.To find substring in given string:
9.Convert lower case to upper case:
10.palindrome the string:
11. Exit
Enter your choice:1
Enter a String:sandipfoundation
String is : sandipfoundation
Lenth of string is : 16
Do you want to continue(0/1) :1
----MEnu---1.Find string lenth:
2.To Copy the string
3.To reverse the string:
4.To compare the string:
5.To concanate the string:
6.To Find occurance of character in perticuler string:
7.To find substring in given string:
8.Convert upper case to lower case:
9.Convert lower case to upper case:
10.palindrome the string:
11. Exit
Enter your choice:2
enter source string: computer
target string is: computer
Do you want to continue(0/1) :1
----MEnu---1.Find string lenth:
2.To Copy the string
3.To reverse the string:
4.To compare the string:
6.To Find occurance of character in perticuler string:
7.To find substring in given string:
8.Convert upper case to lower case:
9.Convert lower case to upper case:
10.palindrome the string:
11. Exit
Enter your choice:3
Enter string :computer
Reverse string is: retupmoc
Do you want to continue(0/1) :1
----MEnu---1.Find string lenth:
2.To Copy the string
3.To reverse the string:
4.To compare the string:
5.To concanate the string:
6.To Find occurance of character in perticuler string:
7.To find substring in given string:
8.Convert upper case to lower case:
9.Convert lower case to upper case:
10.palindrome the string:
11. Exit
Enter your choice:4
Enter first string: abc
Enter second string: ABC
string 1 is greater
Do you want to continue(0/1) :1
----MEnu---1.Find string lenth:
3.To reverse the string:
4.To compare the string:
5.To concanate the string:
6.To Find occurance of character in perticuler string:
7.To find substring in given string:
8.Convert upper case to lower case:
9.Convert lower case to upper case:
10.palindrome the string:
11. Exit
Enter your choice:5
Enter the first string :sandip
Enter the string string :foundation
strings after concatenate is :sandipfoundation
Do you want to continue(0/1) :1
----MEnu---1.Find string lenth:
2.To Copy the string
3.To reverse the string:
4.To compare the string:
5.To concanate the string:
6.To Find occurance of character in perticuler string:
7.To find substring in given string:
8.Convert upper case to lower case:
9.Convert lower case to upper case:
10.palindrome the string:
11. Exit
Enter your choice:6
Enter the first string :sandip
Enter the charcter to find : n
Do you want to continue(0/1) :1
----MEnu---1.Find string lenth:
2.To Copy the string
3.To reverse the string:
4.To compare the string:
5.To concanate the string:
6.To Find occurance of character in perticuler string:
7.To find substring in given string:
8.Convert upper case to lower case:
9.Convert lower case to upper case:
10.palindrome the string:
11. Exit
Enter your choice:7
Enter the first string :computer
Enter sub string to find :ter
substring found
Do you want to continue(0/1) :1
----MEnu---1.Find string lenth:
2.To Copy the string
3.To reverse the string:
4.To compare the string:
5.To concanate the string:
6.To Find occurance of character in perticuler string:
7.To find substring in given string:
8.Convert upper case to lower case:
9.Convert lower case to upper case:
10.palindrome the string:
Enter your choice:8
Enter the string to convert lowercase:COMPUTER
strings after lowercase is :computer
Do you want to continue(0/1) :1
----MEnu---1.Find string lenth:
2.To Copy the string
3.To reverse the string:
4.To compare the string:
5.To concanate the string:
6.To Find occurance of character in perticuler string:
7.To find substring in given string:
8.Convert upper case to lower case:
9.Convert lower case to upper case:
10.palindrome the string:
11. Exit
Enter your choice:9
Enter the string to convert uppercase:computer
strings after uppercase is :COMPUTER
Do you want to continue(0/1) :1
----MEnu---1.Find string lenth:
2.To Copy the string
3.To reverse the string:
4.To compare the string:
5.To concanate the string:
6.To Find occurance of character in perticuler string:
7.To find substring in given string:
9.Convert lower case to upper case:
10.palindrome the string:
11. Exit
Enter your choice:10
Enter a string :madam
string is :madam
string is palindrome
Do you want to continue(0/1) :0
*/
Practical – 3 :
Write a program to perform operations on matrices like
addition, multiplication, saddle point, magic square ,inverse &
transpose etc. using functions & pointers.[Minimum 4 operations]
Objectives:
1. to know how to represent matrices in c language
2. to get knowledge of functions
3. get knowledge of pointers.
Explanation Details
:
Matrix is nothing but collection of rows and columns. It is used
to hold table of data. Following operations are possible on
matrices
−
Addition
−
Multiplication
−
Subtraction
−
Transpose
−
Inverse
−
Saddle point
−
Magic Square
To find inverse we have to follow following process. The data
structure used to implement matrix operations is two dimensional
array.
The rules for addition, subtraction, multiplications and divisions
between matrices are as follows. Let first assume that matrix A
and B are used to construct matrix Z. It must follows that for
•
Addition
:
Z = A + B; zij = aij + bij
•
Subtraction
:
Z = A - B; zij = aij - bij
Addition and Substraction of Matrices
To add or substract matrices these must be of identical order.
This just means that the matrices involved must have the same
number of rows and columns. If they don't have the same number of
rows and columns we cannot add or substract these.
The expression
zij = aij + bij
means "to element in row i, column j of matrix
A
add element in
row i, column j of matrix
B
". If we do this with each element of
A
Multiplication of Matrices
Consider two matrices A and B with the following characteristics:
the number of columns in A equals the number of rows in B. These
are
conformable
with respect to one another, and they can be
multiplied together to form a new matrix Z
The expression
zij = ai1* b1j + ai2* b2j + ai3* b3j + ... aim* bnj
Determinant of Matrix:
Saddle Point
:
Definition
:
“Saddle point in a matrix, is an element which is smaller in
row and maximum in column.”
E.g. Given Matrix, A of
R x C
,
i.e. R = rows and C = columns,
we define a Saddle-Point as
Saddle_Pt (A(i,j)) ≡ A(i,j) is the minimum of Row i and the
maximum of Col j.
e.g.
1
2
3
4
5
6
7
8
9
-- 7 is Saddle_Pt.
at position (3,1)
There may be more than one Saddle-Pt,
Magic Square:
In matrices, a
magic square
of order
n
is an arrangement of
same constant. A
normal
magic square contains the integers from 1
to
n
2. The term "magic square" is also sometimes used to refer to
any of various types of word square.
Normal magic squares exist for all orders
n
≥ 1 except
n
= 2,
although the case
n
= 1 is trivial, consisting of a single cell
containing the number 1. The smallest nontrivial case, shown
below, is of order 3.
The constant sum in every row, column and diagonal is called the
magic constant or magic sum,
M
. The magic constant of a normal
magic square depends only on
n
and has the value
For normal magic squares of order
n
= 3, 4,5, ..., the magic
constants are:
Program:
Write a program to perform operations on matrices like addition,multiplication, saddle point, magic square ,inverse & transpose etc using functions & pointers.*/#include<stdio.h>
/* Protype of functions */
int** create(int m ,int n);
int print(int **a,int m ,int n);
int transpose(int **a,int m ,int n);
int** addmat(int **a,int m1,int n1 ,int **b,int m2,int n2);
int** multmat(int **a,int m1,int n1 ,int **b,int m2,int n2);
int saddle(int **a,int m,int n);
int magic(int **a, int m , int n);
int main()
{ int **a,**b,**c,m1,n1,m2,n2,m3,n3,x;
int opt;
do
{
printf("\n\n\t---MENU---");
printf("\n\n\t1.create first matrix");
printf("\n\n\t2.create second matrix");
printf("\n\n\t3.print first matrix");
printf("\n\n\t4.print second matrix");
printf("\n\n\t5.To perform Addition of matrices");
printf("\n\n\t6.To perform multiplication of matrices");
printf("\n\n\t7.To perform transpose of matrix");
printf("\n\n\t8.To find saddle point in matrix");
printf("\n\n\t9.To check whether the matrix is magic sqare");
printf("\n\n\t1.To find inverse of matrix");
printf("\n\n\t11.EXIT");
printf("\nEnter Your Choice : ");
scanf("%d",&opt);
switch(opt)
{ case 1: printf("\n Enter the size of the matrix :");
scanf("%d%d",&m1,&n1);
a=create(m1,n1);
break;
case 2: printf("\n Enter the size of the matrix :");
scanf("%d%d",&m2,&n2);
b=create(m2,n2);
break;
break;
case 4: print(b,m2,n2);
break;
case 5: if(m1==m2 & n1==n2)
{ c=addmat(a,m1,n1,b,m2,n2);
printf("\nResult=\n");
print(c,m1,n1);
}
else
printf("\n Can not be added ");
break;
case 6: if(n1==m2)
{ c=multmat(a,m1,n1,b,m2,n2);
printf("\nResult=\n");
print(c,m1,n2);
}
else
printf("\n Can not multiply");
break;
case 7: transpose(a,m1,n1);
printf("\nResult=\n");
print(a,m1,n1);
break;
case 8: saddle(a,m1,n1);
break;
case 9:
if(magic(a,m1,n1))
else
printf("\nNot a magic square:");
break;
case 10: inverse(a,m1,n1);
break;
case 11 :
exit(0);
}
printf("\n\n\t Do you want to continue(0/1) :");
scanf("%d",&x);
}while(x==1);
return(0);
}
int ** create(int m ,int n) // matrix stored in a is of size m x n
{ int i,j; int **a;
/*creating a matrix */
a=(int**)malloc(m*sizeof(int*));
for(i=0;i<m;i++)
*(a+i)=(int*)malloc(n*sizeof(int));
printf("\n Enter the data:");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",(*(a+i)+j));
return(a);
}
{ int i,j;
for(i=0;i<m;i++)
{ printf("\n");
for(j=0;j<n;j++)
printf("%5d",*(*(a+i)+j));
}
return(0);
}
int transpose(int **a,int m,int n)
{
int i,j,temp;
if(m==n)
{ for(i=1;i<m;i++)
for(j=0;j<i;j++)
{ temp=*(*(a+i)+j);
*(*(a+i)+j)=*(*(a+j)+i);
*(*(a+j)+i)=temp;
}
}
return(0);
}
int ** addmat(int **a,int m1,int n1 ,int **b,int m2,int n2)
/* Two matrices a and b will be added and the result
will be returned.*/
{ int i,j; int **c;
c=(int**)malloc(m1*sizeof(int*));
for(i=0;i<m1;i++)
if(m1==m2 && n1==n2)
for(i=0;i<m1;i++)
for(j=0;j<n1;j++)
*(*(c+i)+j)=*(*(a+i)+j) + *(*(b+i)+j);
return(c);
}
int** multmat(int **a,int m1,int n1 ,int **b,int m2,int n2)
/* Two matrices a and b will be multiplied and the result
will be returned .*/
{ int i,j,k,temp;
int **c;
c=(int**)malloc(m1*sizeof(int*));
for(i=0;i<m1;i++)
*(c+i)=(int*)malloc(n2*sizeof(int));
if(n1==m2)
{ for(i=0;i<m1;i++)
for(j=0;j<n2;j++)
{ temp=0;
for(k=0;k<n1;k++)
temp=temp + *(*(a+i)+k) * *(*(b+k)+j);
*(*(c+i)+j)=temp;
}
}
return(c);
}
int saddle(int **a,int m,int n) // finds saddle point in matrix
{
for(i=0;i<m;i++)
{
min=*(*(a+i)+0);
col=0;
for(j=1;j<n;j++)
{
if(*(*(a+i)+j)<min)
{
min=*(*(a+i)+j); //minimum term in column
col=j;
}
}
max=*(*(a+0)+col);
for(k=1;k<m;k++)
{
if(*(*(a+k)+col)>max)
{
max=*(*(a+k)+col); //maximum term in row
}
}
if(min==max)
{
printf("\n\n Saddle point is preasent at %d%d with value as %d",i,col,*(*(a+i)+col));
return(1);
}
}
return(0);
}
int magic(int **a,int m ,int n)
{
int rowtotal[20],coltotal[20],i,j,majdiag=0,mindiag=0;
if(m != n)
return(0);
for(i=0;i<n;i++)
rowtotal[i]=coltotal[i]=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{ *(rowtotal+i) += *(*(a+i)+j);
*(coltotal+j) += *(*(a+i)+j);
if(i==j)
majdiag += *(*(a+i)+j);
if(j == n-i-1)
mindiag += *(*(a+i)+j);
}
//values stored in rowtotal[],coltotal[],majdiag and mindiag should be same
if(majdiag != mindiag)
return(0);
for(i=0;i<n;i++)
{
if( *(rowtotal+i) != majdiag)
return(0);
if( *(coltotal+i) != majdiag)
return(0);
return(1);
}
int inverse (int **a,int m ,int n )
{
int i,j,rowno;
float factor,**b;//inverse will be store in matrix b
if(m!=n)
{
printf("\n Not a square matrix... no inverse ");
return;
}
// create the matrix b of size n X 2n
b=(float**)malloc(m*sizeof(float*));
for(i=0;i<2*m;i++)
*(b+i)=(float*)malloc(2*m*sizeof(float));
//copy the matrix a to matrix b and append a unit matrix on the right of b
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{ *(*(b+i)+j)=*(*(a+i)+j);
if(i==j)
*(*(b+i)+j+n)=1;
else
*(*(b+i)+j+n)=0;
}
//go for the reduction of rows
for(rowno=0;rowno<n;rowno++)
{
factor=b[rowno][rowno];
b[rowno][i]/=factor;
for(i=0;i<n;i++)
if(i!=rowno)
{ factor=b[i][rowno];
for(j=0;j<2*n;j++)
b[i][j]=b[i][j]-b[rowno][j]*factor;
}
}
// Display the result
for(i=0;i<n;i++)
{
printf("\n");
for(j=n;j<2*n;j++)
printf("%9.3f",b[i][j]);
}
return(0);
}
/*
OUTPUT :
---MENU---1.create first matrix
2.create second matrix
3.print first matrix
4.print second matrix
5.To perform Addition of matrices
6.To perform multiplication of matrices
8.To find saddle point in matrix
9.To check whether the matrix is magic sqare
10.To find inverse of matrix
11.EXIT
Enter Your Choice : 1
Enter the size of the matrix :3
3
Enter the data:1
2
3
4
5
6
7
8
9
Do you want to continue(0/1) :1
---MENU---1.create first matrix
2.create second matrix
3.print first matrix
4.print second matrix
5.To perform Addition of matrices
6.To perform multiplication of matrices
7.To perform transpose of matrix
8.To find saddle point in matrix
9.To check whether the matrix is magic sqare
11.EXIT
Enter Your Choice : 2
Enter the size of the matrix :3
3
Enter the data:2
3
1
11
10
12
13
5
6
Do you want to continue(0/1) :1
---MENU---1.create first matrix
2.create second matrix
3.print first matrix
4.print second matrix
5.To perform Addition of matrices
6.To perform multiplication of matrices
7.To perform transpose of matrix
8.To find saddle point in matrix
9.To check whether the matrix is magic sqare
10.To find inverse of matrix
11.EXIT
Enter Your Choice : 3
4 5 6
7 8 9
Do you want to continue(0/1) :1
---MENU---1.create first matrix
2.create second matrix
3.print first matrix
4.print second matrix
5.To perform Addition of matrices
6.To perform multiplication of matrices
7.To perform transpose of matrix
8.To find saddle point in matrix
9.To check whether the matrix is magic sqare
10.To find inverse of matrix
11.EXIT
Enter Your Choice : 4
2 3 1
11 10 12
13 5 6
Do you want to continue(0/1) :1
---MENU---1.create first matrix
2.create second matrix
4.print second matrix
5.To perform Addition of matrices
6.To perform multiplication of matrices
7.To perform transpose of matrix
8.To find saddle point in matrix
9.To check whether the matrix is magic sqare
10.To find inverse of matrix
11.EXIT
Enter Your Choice : 5
Result=
3 5 4
15 15 18
20 13 15
Do you want to continue(0/1) :1
---MENU---1.create first matrix
2.create second matrix
3.print first matrix
4.print second matrix
5.To perform Addition of matrices
6.To perform multiplication of matrices
7.To perform transpose of matrix
8.To find saddle point in matrix
9.To check whether the matrix is magic sqare
11.EXIT
Enter Your Choice : 6
Result=
63 38 43
141 92 100
219 146 157
Do you want to continue(0/1) :1
---MENU---1.create first matrix
2.create second matrix
3.print first matrix
4.print second matrix
5.To perform Addition of matrices
6.To perform multiplication of matrices
7.To perform transpose of matrix
8.To find saddle point in matrix
9.To check whether the matrix is magic sqare
10.To find inverse of matrix
11.EXIT
Enter Your Choice : 7
Result=
1 4 7
2 5 8
Do you want to continue(0/1) :1
---MENU---1.create first matrix
2.create second matrix
3.print first matrix
4.print second matrix
5.To perform Addition of matrices
6.To perform multiplication of matrices
7.To perform transpose of matrix
8.To find saddle point in matrix
9.To check whether the matrix is magic sqare
10.To find inverse of matrix
11.EXIT
Enter Your Choice : 8
Saddle point is preasent at 20 with value as 3
Do you want to continue(0/1) :1
---MENU---1.create first matrix
2.create second matrix
3.print first matrix
4.print second matrix
5.To perform Addition of matrices
6.To perform multiplication of matrices
7.To perform transpose of matrix
8.To find saddle point in matrix
9.To check whether the matrix is magic sqare
10.To find inverse of matrix
11.EXITEnter Your Choice : 9
Not a magic square:
Practical No: 4.
Problem Definition
: Write a program to perform following
operations on any database: Add, Delete, Modify, Display,
Search & Sort etc.
Objectives:
1. to know file data structure.
2. To perform different operations.
3. To know library functions available for file operations.
Theory :
In C language we can create database using number of ways like
– using arrays , Link lists, file handling. In this practical
we focus on file handling.
Types of files:
1) Text Files
2) Binary Files
A file is a named area of secondary storage. Secondary storage
is permanent. The
content of a file, unlike the contents of primary memory, which is
volatile, is accessible
after we have turned the power off and back on.
A file is not necessarily stored contiguously on the storage
device: the file may be
fragmented. The operating system controls the fragmentation, if
any.
All peripheral devices allow files to be processed sequentially:
you start at the beginning
of the file and work through each record in turn. One important
advantage of sequential
files is that different records can have different lengths; the
minimum record length is
Sequential files behave as if there were a pointer attached to the
file which always
indicates the next record to be transferred. On devices such as
terminals and printers you
can only read or write in strict sequential order, but when a file
is stored on disc or tape it
is possible to use the REWIND statement to reset this pointer to
the start of the file,
allowing it to be read in again or re-written. On suitable files
the BACKSPACE statement
can be used to move the pointer back by one record so that the
last record can be read
The library function
fopen()
connects a specific file to a
program.
fopen()
returns the
is
FILE *fopen(char file_name[], char mode[]);
The first parameter is a null-byte terminated string containing
the name of the file. The
second parameter is a null-byte terminated string containing the
connection mode.
File opening modes:
"r"
- read from the file,
"w"
- write to the file: if the file exists, truncate its
contents and then write; if the
file does not exist, create a new file and then write to that
file,
"a"
- write to the end of the file: if the file exists, append to
the end of the file; if
the file does not exist, create it and then write.
The mode parameter is a null-byte terminated string (NOT A
CHARACTER). The other
connection modes for text files are
"r+"
- opens the file for reading and possibly writing,
"w+"
- opens the file for writing and possibly reading; if the
file exists, truncates its contents and then writes to the file;
if the file does not exist, creates a new file
and then writes to that file,
"a+"
- opens the file for writing to the end of the file and
possibly reading; if the file exists, appends to the end of the
file; if the file does not exist, creates it and then writes to
the file.
fopen
returns
NULL
if the attempt to connect to the file fails.
fopen
can fail for lack of
permission, premature removal of the secondary storage medium,
device full, etc.
The library function
fclose()
disconnects a file from a program.
fclose()
takes as its only
parameter the address of the file data structure. The prototype
for
fclose()
is
int fclose(FILE *);
If the program opened the file for writing,
fclose()
writes any
data remaining in the file
stream's buffer to the file and concludes by appending an end of
file mark immediately
after the last character. If the program opened the file for
reading,
fclose()
ignores any
data left in the file stream's buffer and closes the connection.
For example, to open a file
named
alpha.txt
for writing and then to close the file, we write
Opening and closing a file
#include <stdio.h>
int main( ) {
FILE *fp = NULL;
fp = fopen("alpha.txt","w");
if (fp != NULL) {
/* we will add statements here later */
fclose(fp);
}
else
printf("Failed to open file\n");
return 0;
}
fclose()
returns 0 if successful,
EOF
if unsuccessful.
fclose()
storage medium is full, an I/O error occurs or the medium has been
prematurely
removed.
WRITING TO A FILE :
The library function
fprintf()
sends data from primary memory to a
connected file under
format control. The prototype for
fprintf()
is
int fprintf(FILE *, char [], ...);
The first parameter receives the address of the file connection
data structure. The second
parameter is the format string containing the text to be written
directly to the file and the conversion specifiers, if any, to be
applied to the data values received in the parameters
following the format string. Note the similarity between the
fprintf()
and the
printf()
library functions.
For example:
Writing to a File
#include <stdio.h>
int main( ) {
FILE *fp = NULL;
char phrase[] = "My name is Arnold";
fp = fopen("alpha.txt","w");
if (fp != NULL) {
fprintf(fp, "%s\n", phrase);
fclose(fp);
} else
printf("Failed to open file\n");
}
The library function
fputc()
writes a single character to a file.
The prototype for
fputc()
is:
int fputc(int ch, FILE *fp);
ch
is the character to be written and
fp
is the address of the
connection data structure for
the destination file.
fputc()
returns the character written, or
EOF
in the event of an error.
The library function
fputs()
writes a null-byte terminated
character to a file. The
prototype for
fputs()
is:
int fputs(char str[], FILE *fp);
str
is the string to be written and
fp
is the address of the
connection data structure for the
destination file.
fputs()
returns a non-negative value if
successful, or
EOF
in the event of an error.
READING FROM :
The library function
fscanf()
reads a sequence of bytes from a
connected file into primary
memory under format control. The prototype for
fscanf()
is
int fscanf(FILE *, char [], ...);
The first parameter receives the address of the file connection
data structure. The second
parameter is the format string containing the conversion
specifiers to be applied to the