Practical File 2
Practical File 2
: 18DCS040
PRACTICAL 18
Aim:
Write a program to find whether two given strings are permutations of each other. A
string str1 is a permutation of str2 if all the characters in str1 appear the same number of
times in str2 and str2 is of the same length as str1. For example if two strings are kindness
and ksnisden the answer is Yes.
Program code:
#include<stdio.h>
#include<conio.h>
void main()
int flag=0,i,j,count1=0,count2=0;
char s1[100],s2[100],temp;
printf("\nDate : 10-09-2018\n”);
DEPSTAR(CSE) Page 58
Computer Concepts and Programming[CE141] ID No. : 18DCS040
{
for(j=0;j<count1-1;j++)
{
if(s1[j]>s1[j+1])
{
temp=s1[j];
s1[j]=s1[j+1];
s1[j+1]=temp;
}
}
}
for(i=0;i<count2;i++)
{
for(j=0;j<count2-1;j++)
{
if(s2[j]>s2[j+1])
{
temp=s2[j];
s2[j]=s2[j+1];
s2[j+1]=temp;
}
}
}
}
else
printf("\nLENGTH OF STRINGS ARE NOT EQUAL.");
for(i=0;i<count1;i++)
DEPSTAR(CSE) Page 59
Computer Concepts and Programming[CE141] ID No. : 18DCS040
{
if(s1[i]==s2[i])
flag=1;
else
{
flag=0;
break;
}
}
if(flag==1)
printf("\nGiven strings are permutations of each other.");
else
printf("\nGiven strings are not permutations of each other.");
getch();
}
Output:
DEPSTAR(CSE) Page 60
Computer Concepts and Programming[CE141] ID No. : 18DCS040
HOMEWORK-2
AIM:-
Write a program that would sort a list of names of fruits in alphabetical order.
For example 5 names Orange, Pineapple, Grapes, Banana, Mango should sort the names as
follows:
Banana
Grapes
Mango
Orange
Pineapple
PROGRAM CODE:-
#include<stdio.h>
#include<string.h>
void main()
int i,j,n;
char a[100][100];
char temp[100];
clrscr();
printf("\nDate : 10-09-2018\n”);
scanf("%d",&n);
DEPSTAR(CSE) Page 61
Computer Concepts and Programming[CE141] ID No. : 18DCS040
for (i=0;i<=n;i++)
gets(a[i]);
for (i=0;i<=n;i++)
for (j=i+1;j<=n;j++)
if (strcmp(a[i],a[j])>0)
strcpy(temp,a[i]);
strcpy(a[i],a[j]);
strcpy(a[j],temp);
printf("\nAlphabetical order\n");
for (i=0;i<=n;i++)
puts(a[i]);
getch();
DEPSTAR(CSE) Page 62
Computer Concepts and Programming[CE141] ID No. : 18DCS040
OUTPUT:-
CONCLUSION:-
In this practical, I learnt the use of strcmp() and strcpy() functions which are stored in the header
file <string.h>.
Here, the strings are stored in an array and then each string is compared character by character
and sorted in ascending order.
DEPSTAR(CSE) Page 63
Computer Concepts and Programming[CE141] ID No. : 18DCS040
PRACTICAL 19
Aim:
Write a Program to print the table of Squares and Cubes of 1 to 10. The Program uses the
following four function:
Functions Category
printline(): draws the Function with No Arguments, No return
line using “ - ‟ character. Type
printnum() : prints Function with Arguments ,No Return type
number, square and
cube.
square() : computes Function with Arguments, with Return
square of a number. Type
cube() : computes cube Function with Arguments, with Return
of a number. Type
Program Code:
#include<stdio.h>
#include<conio.h>
void printline()
{
printf(“---------------------------");
}
int square(int n)
{
return n*n;
}
int cube(int n)
{
return n*n*n;
}
void printnum(int n1,int n2,int n3)
DEPSTAR(CSE) Page 64
Computer Concepts and Programming[CE141] ID No. : 18DCS040
{
printf("|%6d |%6d | %6d |\n",n1,square(n2),cube(n3));
}
void main()
{
int i=0;
printf("Name : Yash Makadiya");
printf("\nDate : 10-09-2018\n”);
printline();
printf("\n| Number | Square | Cube |\n");
printline();
printf("\n");
for(i=0;i<11;i++)
printnum(i,i,i);
printline();
getch();
}
DEPSTAR(CSE) Page 65
Computer Concepts and Programming[CE141] ID No. : 18DCS040
Output:
Conclusion:
In this program we used the concept of functions and various typed of functions and obtaines
the desirable output.
DEPSTAR(CSE) Page 66
Computer Concepts and Programming[CE141] ID No. : 18DCS040
HOMEWORK-3
Aim:
Write a program to calculate nCr using Function with No arguments But with Return type.
(Hint: nCr = n! / ((r!) (n – r)!)).
Program Code:
#include<stdio.h>
#include<string.h>
int fact(int );
float nCr();
void main()
{
float result;
clrscr();
result=nCr();
printf("Name : Yash Makadiya");
printf("\nID No.: 18DCS040");
printf("\nDate : 17-09-2018\n”);
printf("\n%.0f",result);
getch();
}
int fact(int a)
{
int i,factorial=1;
for(i=1;i<=a;i++)
{
factorial*=i;
}
DEPSTAR(CSE) Page 67
Computer Concepts and Programming[CE141] ID No. : 18DCS040
return factorial;
}
float nCr()
{
int n,r;
float com;
printf(“\nEnter the n of nCr:”);
scanf("%d",&n);
printf(“\nEnter the r of nCr:”);
scanf("%d",&r);
com=fact(n)/(fact(r)*fact(n-r));
return com;
}
Output:
Conclusion:
In this program we have used factorial user defined function to find nCr by making another user
defined function of nCr.
DEPSTAR(CSE) Page 68
Computer Concepts and Programming[CE141] ID No. : 18DCS040
PRACTICAL 20
Aim:
Write a program to pass a number entered through keyboard as an argument to user-
defined functions and find the factors of a number and check whether the factors are prime
or not using Nested Functions.
Program code:
#include<stdio.h>
#include<conio.h>
int prime(int n)
{
int i,flag=0;
for(i=2;i<n;i++)
{
if(n%i==0)
{
flag=1; break;
}
}
return flag;
}
void factor(int n)
{
int i=0,j=0;
for(i=2;i<n;i++)
{
if(n%i==0)
{
DEPSTAR(CSE) Page 69
Computer Concepts and Programming[CE141] ID No. : 18DCS040
Output:
Conclusion:
In this program we learnt the use of nested functions.
DEPSTAR(CSE) Page 70
Computer Concepts and Programming[CE141] ID No. : 18DCS040
PRACTICAL 21
Aim:
Write a program to generate Fibonacci series using Recursive Function. In a Fibonacci
sequence the sum of two successive terms gives the third term.
1 1 2 3 5 8 13 ….
Program code:
#include<stdio.h>
#include<conio.h>
int fibonacci(int n)
{
if(n<2)
return n;
return fibonacci(n-1)+fibonacci(n-2);
}
void main()
{
int num,i;
printf("Name : Yash Makadiya");
printf("\nID No.: 18DCS040");
printf("\nDate : 24-09-2018\n”);
printf("\nEnter number of terms : ");
scanf("%d",&num);
for(i=1;i<=num;i++)
printf("%d\t",fibonacci(i));
getch();
}
DEPSTAR(CSE) Page 71
Computer Concepts and Programming[CE141] ID No. : 18DCS040
Output:
Conclusion:
In this program we learnt the use of recursive function.
DEPSTAR(CSE) Page 72
Computer Concepts and Programming[CE141] ID No. : 18DCS040
PRACTICAL 22
Aim:
Write a Program to compute the standard Deviation of N Numbers using Arrays &
Function.
Program code:
#include<stdio.h>
#include<math.h>
int mean(int c[],int n)
{
int i;
float sum=0;
for(i=0;i<n;i++)
sum=sum+c[i];
return (sum/n);
}
int std_dev(int c[],int n)
{
int i=0,x,sum=0.0;
x=mean(c,n);
for(i=0;i<n;i++)
{
sum=sum+(c[i]-x)*(c[i]-x);
}
return (sqrt(sum/n));
}
DEPSTAR(CSE) Page 73
Computer Concepts and Programming[CE141] ID No. : 18DCS040
void main()
{
int i,n,arr[50];
printf("Name : Yash Makadiya");
printf("\nID No.: 18DCS040");
printf("\nDate : 24-09-2018\n”);
printf("\nEnter number of entries : ");
scanf("%d",&n);
printf("\nEnter numbers :\n");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("\nThe standard deviations is : %d",std_dev(arr,n));
getch();
Output:
Conclusion:
In this program we learnt the concept of passing arrays to function.
DEPSTAR(CSE) Page 74
Computer Concepts and Programming[CE141] ID No. : 18DCS040
HOMEWORK-4
Aim:
Write a Program to reverse a string using Recursive Function and check whether it is
palindrome or not.
Program code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int l = 0;
int h = strlen(str) - 1;
while (h > l)
if (str[l++] != str[h--])
return;
printf("\n %s is a palindrome",str);
char temp;
DEPSTAR(CSE) Page 75
Computer Concepts and Programming[CE141] ID No. : 18DCS040
temp=str[index];
str[index]=str[size-index];
str[size-index]=temp;
if(index==size/2)
return ;
revstring(str,index+1,size);
void main()
char str[100];
int length;
clrscr();
gets(str);
length=strlen(str);
revstring(str,0,length-1);
isPalindrome(str);
getch();
DEPSTAR(CSE) Page 76
Computer Concepts and Programming[CE141] ID No. : 18DCS040
Output:
Conclusion:
In this program we have made two user-defined functions to find
whether a string is palindrome or not.
DEPSTAR(CSE) Page 77
Computer Concepts and Programming[CE141] ID No. : 18DCS040
PRACTICAL 23
Aim:
Write a Program to find the upper triangle in the given matrix. Consider the following 4 x
4 Matrix.
XXXX
0 XXX
0 0 XX
0 0 0 X
If all the elements denoted by X are non-zero then the matrix has upper triangle. For the
upper triangle, all the elements of principle diagonal and above must be non – zero. Pass
two dimensional arrays to the function.
Program code:
#include<stdio.h>
#include<conio.h>
void utm(int a[4][4])
{
int i,j,count=0;
for(i=0;i<4;i++)
{
for(j=0+i;j<4;j++)
{
if(a[i][j]!=0)
{
count++;
}
}
}
if(count!=10)
DEPSTAR(CSE) Page 78
Computer Concepts and Programming[CE141] ID No. : 18DCS040
DEPSTAR(CSE) Page 79
Computer Concepts and Programming[CE141] ID No. : 18DCS040
getch();
}
Output:
Conclusion:
In this program we used concept of passing two dimensional arrays to function.
DEPSTAR(CSE) Page 80
Computer Concepts and Programming[CE141] ID No. : 18DCS040
PRACTICAL 24(A)
Aim:
Create a Structure called library to hold accession number, title of the book ,author name,
price of the book and flag indicating whether the book is issued or not.(flag = 1 if the book
is issued , flag = 0 otherwise). Write a program to enter data of one book and display the
data. Write this same program with Union also.
Program code:
#include<stdio.h>
#include<conio.h>
struct library
{
int acc_no;
char title[25],author[50];
float price;
}book;
void main()
{
int flag=0;
printf("Name : Yash Makadiya");
printf("\nID No.: 18DCS040");
printf("\nDate : 08-10-2018\n”);
printf("\nEnter book accession number : ");
scanf("%d",&book.acc_no);
fflush(stdin);
printf("\nEnter title of book : ");
gets(book.title);
printf("\nEnter author of book : ");
gets(book.author);
DEPSTAR(CSE) Page 81
Computer Concepts and Programming[CE141] ID No. : 18DCS040
DEPSTAR(CSE) Page 82
Computer Concepts and Programming[CE141] ID No. : 18DCS040
Output:
Conclusion:
In this program we learnt the concept of Structures.
DEPSTAR(CSE) Page 83
Computer Concepts and Programming[CE141] ID No. : 18DCS040
PRACTICAL 24(B)
Aim:
Create a Union called library to hold accession number, title of the book, author name, price
of the book and flag indicating whether the book is issued or not.(flag = 1 if the book is issued
, flag = 0 otherwise). Write a program to enter data of one book and display the data.
Program Code:
#include<stdio.h>
#include<conio.h>
union library
{
int acc_no;
char title[25],author[50];
float price;
}book;
int main()
{
int flag=0;
printf("Name : Yash Makadiya");
printf("\nID No.: 18DCS040");
printf("\nDate : 08-10-2018\n");
printf("\nEnter book accession number : ");
scanf("%d",&book.acc_no);
fflush(stdin);
printf("\nEnter title of book : ");
gets(book.title);
printf("\nEnter author of book : ");
gets(book.author);
DEPSTAR(CSE) Page 84
Computer Concepts and Programming[CE141] ID No. : 18DCS040
DEPSTAR(CSE) Page 85
Computer Concepts and Programming[CE141] ID No. : 18DCS040
Output:
Conclusion:
DEPSTAR(CSE) Page 86
Computer Concepts and Programming[CE141] ID No. : 18DCS040
PRACTICAL 25
Aim:
Define a structure called Result for students. Structure will have members like Roll
number, marks for three subjects and total of three subjects. Write a program to enter
data for 5 students and display the merit list of students. Use Array of Structures. For
example, if Roll No and marks of three subjects of each student are entered through the
keyboard.
Program code:
#include<stdio.h>
#include<conio.h>
struct result
int roll_no;
int marks[3];
int total;
};
void main()
int i,j,temp,flag[3],temp1;
clrscr();
DEPSTAR(CSE) Page 87
Computer Concepts and Programming[CE141] ID No. : 18DCS040
scanf("%d",&s[i].roll_no);
flushall();
scanf(" %d",&s[i].marks[0]);
flushall();
scanf(" %d",&s[i].marks[1]);
flushall();
scanf(" %d",&s[i].marks[2]);
s[i].total=s[i].marks[0]+s[i].marks[1]+s[i].marks[2];
for(j=0;j<5;j++)
for(i=0;i<5-j;i++)
temp=s[i].total;
s[i].total=s[i+1].total;
s[i+1].total=temp;
temp1=s[i].roll_no;
DEPSTAR(CSE) Page 88
Computer Concepts and Programming[CE141] ID No. : 18DCS040
s[i].roll_no=s[i+1].roll_no;
s[i+1].roll_no=temp1;
flag[0]=s[i].marks[0];
s[i].marks[0]=s[i+1].marks[0];
s[i+1].marks[0]=flag[0];
flag[1]=s[i].marks[1];
s[i].marks[1]=s[i+1].marks[1];
s[i+1].marks[1]=flag[1];
flag[2]=s[i].marks[2];
s[i].marks[0]=s[i+1].marks[2];
s[i+1].marks[2]=flag[2];
for(i=0;i<5;i++)
getch();
DEPSTAR(CSE) Page 89
Computer Concepts and Programming[CE141] ID No. : 18DCS040
Output:
Conclusion:
In this program we learnt the concept of array of structure.
DEPSTAR(CSE) Page 90
Computer Concepts and Programming[CE141] ID No. : 18DCS040
PRACTICAL 26
Aim:
Write a program to read and display information of salary of an employee using Structure
within a Structure. Outer structure contains members like name of employee, designation,
department name, basic pay and inner structure contains dearness allowance, house_rent
allowance and city_allowance. Calculate the total salary of one employee.
Program code:
#include<stdio.h>
#include<conio..h>
struct salary
{
char name[20],designation[10],dept[10];
int basic_pay;
struct allowance
{
int da,hra,ca;
}all;
}emp;
void main()
{
printf("Name : Yash Makadiya");
printf("\nID No.: 18DCS040");
printf("\nDate : 15-10-2018\n");
printf("\nEnter employee name : ");
gets(emp.name);
printf("Enter employee designation : ");
gets(emp.designation);
printf("Enter employee department : ");
DEPSTAR(CSE) Page 91
Computer Concepts and Programming[CE141] ID No. : 18DCS040
gets(emp.dept);
printf("Enter employee basic pay : ");
scanf("%d",&emp.basic_pay);
printf("Enter dearness allowance : ");
scanf("%d",&emp.all.da);
printf("Enter house rent allowance : ");
scanf("%d",&emp.all.hra);
printf("Enter city allowance : ");
scanf("%d",&emp.all.ca);
printf("\n\nName : ");
puts(emp.name);
printf("\nDesignation : ");
puts(emp.designation);
printf("\nDepartment : ");
puts(emp.dept);
printf("\nTotal Salary : %d",emp.basic_pay+emp.all.da+emp.all.ca+emp.all.hra);
getch();
}
DEPSTAR(CSE) Page 92
Computer Concepts and Programming[CE141] ID No. : 18DCS040
Output:
Conclusion:
In this program we have learnt the concept of nested structures.
DEPSTAR(CSE) Page 93
Computer Concepts and Programming[CE141] ID No. : 18DCS040
PRACTICAL 27
Aim:
Define a structure named Date that contains three members day, month and Year. Write a
program that compares two given dates. If the dates are equal then display message as
“Equal” otherwise “Unequal”. Write a function Check_Date to check whether the entered
date is proper or not. The date is proper if day is between 1 and 31, month is between 1 and
12 and year is between 1000 and 9999. (Structures & Functions)
Program code:
#include<stdio.h>
#include<conio.h>
struct date
{
int day;
int month;
int year;
}d1,d2;
void Check_Date(struct date );
void main()
{
clrscr();
printf("Name : Yash Makadiya");
printf("\nID No.: 18DCS040");
printf("\nDate : 15-10-2018\n");
printf("\nEnter the first date:");
printf("\nEnter day:");
scanf("%d",&d1.day);
printf("\nEnter month:");
scanf("%d",&d1.month);
DEPSTAR(CSE) Page 94
Computer Concepts and Programming[CE141] ID No. : 18DCS040
printf("\nEnter year:");
scanf("%d",&d1.year);
Check_Date(d1);
DEPSTAR(CSE) Page 95
Computer Concepts and Programming[CE141] ID No. : 18DCS040
Output:
Conclusion:
In this program we learnt the concept of structures to function.
DEPSTAR(CSE) Page 96
Computer Concepts and Programming[CE141] ID No. : 18DCS040
PRACTICAL 28
Aim:
Write a program to perform following operations on two integer pointers.
1. Addition 2. Subtraction 3. Increment 4. Swaping of two numbers
5. Max and min of two numbers
Program code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int a,b,choice,*p1,*p2;
clrscr();
printf("Name : Yash Makadiya");
printf("\nID No.: 18DCS040");
printf("\nDate : 22-10-2018\n");
printf("\nPress the given integers to perform the specific functions");
printf("\n1. Addition\n2. Subtraction\n3. Increment\n4. Swaping of two numbers\n5. Max and
min of two numbers\n");
scanf("%d",&choice);
printf("\nEnter any two integers:\n");
scanf("\n%d %d",&a,&b);
p1=&a;
p2=&b;
switch(choice)
{
case 1:{
DEPSTAR(CSE) Page 97
Computer Concepts and Programming[CE141] ID No. : 18DCS040
int sum;
sum=*p1+*p2;
printf("\nThe Addition of two integers is: %d",sum);
break;
}
case 2:{
int sub;
sub=*p1-*p2;
printf("\nThe Subtraction of two integers is: %d",sub);
break;
}
case 3:{
(*p1)++;
(*p2)++;
printf("\nThe increamented numbers are %d and %d",*p1,*p2);
break;
}
case 4:{
int temp;
*p1=temp;
*p1=*p2;
*p2=temp;
printf("\nThe swapped numbers are: %d %d",*p1,*p2);
break;
}
case 5:{
if(*p1<*p2)
DEPSTAR(CSE) Page 98
Computer Concepts and Programming[CE141] ID No. : 18DCS040
Output:
Conclusion:
In this program we learnt the basic concept of pointers and functionality of pointers.
DEPSTAR(CSE) Page 99
Computer Concepts and Programming[CE141] ID No. : 18DCS040
PRACTICAL 29
Aim:
Write a program to read the marks of 10 students for the subject CE141 Computer
concepts and Programming and computes the number of students in categories
FAIL(below 40), PASS(40-59), FIRST CLASS(60-69) and DISTINCTION(70 or above)
using Pointers and Arrays.
Program code:
#include<stdio.h>
#include<conio.h>
void main()
int marks[10],d=0,ft=0,p=0,f=0,i,*ptr;
ptr = marks;
clrscr();
for(i=0;i<10;i++,ptr++)
scanf(" %d",ptr);
if((*ptr)>=70)
d++;
if((*ptr)<=69&&(*ptr)>=60)
ft++;
if((*ptr)<=59&&(*ptr)>=40)
p++;
if((*ptr)<40)
f++;
getch();
Output:
Conclusion:
In this program we learnt the concept of pointer to an array.
PRACTICAL 30
Aim:
Write a program that uses an array of pointers to strings str[ ]. Receive two strings str1
and str2 and check if str1 is embedded in any of the strings in str[ ]. If str1 is found, then
replace it with str2.
char *str[ ] = { "We will teach you how to...", "Move a mountain", "Level a building",
"Erase the past", "Make a million", "...all through C!" } ;
Program code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char *ptr, *p, str1[30], str2[30], *temp;
int flag;
char *str[6] = {
"We will teach you how to...",
"Move a mountain",
"Level a building",
"Erase the past",
"Make a million",
"...all through C!"
};
int i;
clrscr();
printf("Name : Yash Makadiya");
printf("\nID No.: 18DCS040");
printf("\nDate : 22-10-2018\n");
for(i=0;i<6;i++)
{
ptr=str[i];
puts(ptr);
}
else
for(i=0;i<6;i++)
{
ptr=str[i];
puts(ptr);
}
getch();
}
Output:
Conclusion:
In this program we have learned the use of array of pointers to string.
PRACTICAL 31(A)
Aim:
Write a program which performs the following tasks:
− initialize an integer array of 10 elements in main( )
− pass the entire array to a function modify( )
− in modify( ) multiply each element of array by 3
− return the control to main( ) and print the new array elements in main( )
Above program is done in call by address.
Program code:
#include<stdio.h>
#include<conio.h>
void modify(int p[]);
void main()
{
int a[10],i,*p;
p=a;
clrscr();
printf("Name : Yash Makadiya");
printf("\nID No.: 18DCS040");
printf("\nDate : 22-10-2018\n");
printf("\nEnter any 10 values:\n");
for(i=0;i<10;i++)
{
scanf("%d",p);
p++;
}
modify(a);
for(i=0;i<10;i++)
{
printf("a[%d] = %d\n",i,a[i]);
}
getch();
}
void modify(int p[])
{
int i;
for(i=0;i<10;i++)
{
p[i]=p[i]*3;
}
}
Output:
Conclusion:
In this program we have learned use of call by reference in user-defined functions.
PRACTICAL 31(B)
Aim:
Write a program which performs the following tasks:
− initialize an integer array of 10 elements in main( )
− pass the entire array to a function modify( )
− in modify( ) multiply each element of array by 3
− return the control to main( ) and print the new array elements in main( )
Above program is done in call by value.
Program code:
#include<stdio.h>
#include<conio.h>
int modify(int );
void main()
{
int a[10],i;
clrscr();
printf("Name : Yash Makadiya");
printf("\nID No.: 18DCS040");
printf("\nDate : 22-10-2018\n");
printf("\nEnter any 10 values:\n");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<10;i++)
{
a[i]=modify(a[i]);
printf("a[%d] = %d\n",i+1,a[i]);
}
getch();
}
int modify(int p)
{
return p*3;
}
Output:
Conclusion:
In this program we have learned use of call by value in user-defined functions.
PRACTICAL 32(A)
Aim:
Write the output of the given program code. (Pointers to Functions)
Program code:
#include<stdio.h>
#include<conio.h>
void display();
void main()
void (*func_ptr)();
clrscr();
func_ptr=display;
(*func_ptr)();
getch();
void display()
Output:
Conclusion:
In this program we have learned the use of pointer to function.
PRACTICAL 32(B)
Aim:
Write the output of the given program code. (Functions Returning Pointers)
Program code:
#include<stdio.h>
#include<conio.h>
char *copy (char*,char *);
void main()
{
char *str;
char source[] = "Kindness";
char target[10];
str=copy(target,source);
printf("Name : Yash Makadiya");
printf("\nID No.: 18DCS040");
printf("\nDate : 23-10-2018\n");
printf("%s\n",str);
getch;
}
char *copy(char *t,char *s)
{
char * r; r = t;
while(*s!='\0')
{
*t=*s;
t++;
s++;
}
*t='\0';
return(r);
}
Output:
Conclusion:
In this program we have learned the use of Functions Returning Pointers.
PRACTICAL 33
Aim:
An automobile company has serial number engine parts starting from AA0 to FF9. The
other characteristics of parts to be specified in structure are year of manufacturing,
material and quantity manufactured.
(b) Write a program using pointer to retrieve information on parts with serial numbers
between BB1 and CC6. (Pointers and Structures)
Program code:
#include<stdio.h>
#include<conio.h>
struct part
char sr_no[10];
char material[30];
int year;
int quantity;
}p[60],*ptr;
void main()
int i;
char str1='B',str2='C';
clrscr();
ptr=p;
for(i=0;i<30;i++)
gets(ptr->sr_no); flushall();
gets(ptr->material); flushall();
scanf("%d",&ptr->year); flushall();
scanf("%d",&ptr->quantity); flushall();
ptr++;
ptr=&p[12];
for(i=12;i<28;i++)
ptr++;
getch();
Output:
Conclusion:
In this program we learnt the concept of structure to pointers.
PRACTICAL 34
Aim:
Write a program that takes contents of a file and copy them into another file and print it on
the screen. Use feof () functions to detect the end of file and ferror() function to detect if
there is an error in opening the file.
Program code:
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp1,*fp2;
char filename[10],c;
printf("Name : Yash Makadiya");
printf("\nID No.: 18DCS040");
printf("\nDate : 23-10-2018\n");
printf("\nEnter the file name whose content is to be copied:\n");
scanf("%s",filename);
fp1=fopen(filename,"r");
if(ferror(fp1))
{
printf("\nFile not found");
exit(1);
}
printf("\nEnter the file name in which content is to be copied:\n");
scanf("%s",filename);
fp2=fopen(filename,"w");
if(ferror(fp2))
{
printf("\nFile openning error");
exit(1);
}
c=fgetc(fp1);
while(1)
{
fputc(c,fp2);
c=fgetc(fp1);
if(feof(fp1))
break;
}
fclose(fp1);
fclose(fp2);
printf("\nCopying in the file %s is done",filename);
getch();
}
Output:
Conclusion:
In this program we learnt the basic concepts of file handling and error functions.
PRACTICAL 35
Aim:
Write a program to create a file named ALPHABETS which consists of all 26 letters
ABC…XYZ and prints the contents of the file in reverse order ZYX….CBA on the screen.
Use the function ftell(), fseek() and rewind().
Program code:
#include<stdio.h>
#include<conio.h>
void main()
FILE *f1;
char ch;
int count=0,i=0;
f1=fopen("Alphabet.txt","w");
while((ch=getchar())!='*')
putc(ch,f1);
fclose(f1);
f1=fopen("Alphabet.txt","r");
fseek(f1,0,SEEK_END);
count=ftell(f1);
while(i!=count)
i++;
fseek(f1,-(i),SEEK_END);
printf("%c",fgetc(f1));
getch();
Output:
Conclusion:
In this program we learnt the functions like fseek(), ftell() which are useful for working on files.
PRACTICAL 36
Aim:
Two files Data1 and Data2 contains sorted list of integers. Write a program to produce file
Data3 which holds a single sorted, merge list of these two list. Use command line argument
to specify the file name.
Program code:
#include<stdio.h>
#include<conio.h>c
FILE *f1,*f2,*f;
inti,a[30],b[30];
clrscr();
f1=fopen("Data1.txt","r");
f2=fopen("Data2.txt","r");
f=fopen(argv[1],"w");
for(i=0;i<10;i++)
fscanf(f1,"%d",&a[i]);
printf("%d\t",a[i]);
for(i=0;i<10;i++)
fscanf(f2,"%d",&b[i]);
printf("%d\t",b[i]);
for(i=0;i<10;i++)
fprintf(f,"%d\t",a[i]);
fprintf(f,"%d\t",b[i]);
fclose(f1);
fclose(f2);
fclose(f);
f=fopen(argv[1],"r");
for(i=0;i<20;i++)
fscanf(f,"%d",&a[i]);
printf("%d\t",a[i]);
fclose(f);
getch();
Output:
Conclusion:
In this program we learnt the concept of argc and argv and how to work on command line
arguments.
PRACTICAL 37
Aim:
Write a program to enter N numbers into array and sort the second half of the array using
function sort(). Enter the size of the array through keyboard. (Dynamic Array). Use malloc
() to allocate memory and use free() to free the memory after the use.
Program code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void sort(int* ,int );
void main()
{
int *ptr,*p1,i,n;
clrscr();
printf("Name : Yash Makadiya");
printf("\nID No.: 18DCS040");
printf("\nDate : 25-10-2018\n");
printf("\nEnter the number of memory blocks to be alloted:");
scanf("%d",&n);
ptr=(int*)malloc(n*sizeof(int));
printf("\nEnter the elements:\n");
for(i=0;i<n;i++)
{
scanf("%d",ptr+i);
}
p1=ptr;
sort(p1,n);
getch();
}
void sort(int *p,int k)
{
int i,j,temp;
for(i=k/2;i<k;i++)
{
for(j=k/2;j<k-1;j++)
{
if(*(p+j)>(*(p+j+1)))
{
temp=*(p+j+1);
*(p+j+1)=*(p+j);
*(p+j)=temp;
}
}
}
printf("\nSorted elements are:\n");
for(i=0;i<k;i++)
{
printf("\n %d",(*(p+i)));
}
}
Output:
Conclusion:
In this program we learnt to to deal with memory dynamically.
PRACTICAL 38
Aim:
Write a program using to store a character string in a block of memory space created by
calloc () and then modify the same to store a larger string using realloc () function.
(Dynamic Array).
Program code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
int *ptr,*p,n;
char ch;
clrscr();
ptr=(char*)calloc(6,sizeof(char));
printf("Name : Yash Makadiya");
printf("\nID No.: 18DCS040");
printf("\nDate : 25-10-2018\n");
printf("\nEnter the string:");
gets(ptr);
printf("\nThe entered string is:");
puts(ptr);
fflush(stdin);
printf("\nDo you want to increase size of name(y/n):");
scanf("%c",&ch);
if(ch=='y')
{
printf("\nEnter the new size:");
scanf("%d",&n);
p=(char*)realloc(ptr,n);
fflush(stdin);
printf("\nEnter the next string:");
gets(p);
printf("\nThe entered string is:");
puts(p);
}
free(ptr);
free(p);
getch();
}
Output:
Conclusion:
In this program we learnt the concept of dynamic memory allocation along with functions like
calloc() and realloc().