Anda di halaman 1dari 49

# ARRAY

## One Dimensional Array

1. Write a program in C, you accept an Array and perform the following task
a. Create an array, length is given by user,
b. Insert an element into a position. Both position & element will be given by the
user,
c. Delete an element from a particular position of the array,
d. Search an element in an array,
e. Find maximum & minimum element in an array,
f. Sort the entire array,
g. Display the entire array.

Ans:
#include<stdio.h>
int a[100],n;
void create(void);
void insert(void);
void delet(void);
void search(void);
void maxmin(void);
void ascending(void);
void descending(void);
void display(void);

int main(void)
{
int i;
create();

## //create an array function call

do
{
printf("\n\n\n1 for INSERT an element\n2 for DELETE an element\n3 for Search an
element\n4 for Find Maximum and Minimum element\n5 for Sort in Ascending order\n6 for Sort in
Descending Order\n7 for display the Array\n8 for EXIT");
printf("\n\nEnter the above option:");
scanf("%d",&i);
switch(i)
{
case 1:
insert();
break;
case 2:
delet();
break;
case 3:
search();
break;
case 4:
1

maxmin();
break;
case 5:
ascending();
break;
case 6:
descending();
break;
case 7:
display();
break;
case 8:
break;
default:
Try Again");
}
}while(i!=8);
}
void insert(void)
{
int i=n-1,p,v;
printf("\nEnter the Position of the array:");
scanf("%d",&p);
if(p<=(n-1))
{
printf("\nEnter the new element (number) of this position:");
scanf("%d",&v);
p-=1;
n+=1;
while(i>=p)
{
a[i+1]=a[i];
i-=1; //i--;
}
a[p]=v;
printf("\n\n%d is Inserted in the ARRAY",v);
}
else
printf("\n\nYou Enter wrong position.\n........Try again........");
}
void create(void)
{
int i;
printf("\nEnter how many elements you want to enter:");
scanf("%d",&n);
printf("\n.....Enter the Array element one by one.....\n");
for(i=0;i<n;i++)
{
printf("Enter the element no %d:",i+1);
scanf("%d",&a[i]);
}
2

## printf("\n\n...The above Array is...\n");

i=0;
while(i<n)
{
printf("\nElement no %d is %d",i+1,a[i]);
i++;
}
}

void delet(void)
{
int p,i;
printf("\nEnter the Position of the array:");
scanf("%d",&p);
if(p<=(n-1))
{
i=p;
printf("\n%d element is deleted from ARRAY\n\n",a[p-1]);
while(i<=(n-1))
{
a[i-1]=a[i];
i++;
}
n=n-1;
}
else
printf("\n\nYou enter wrong position.\n........Try again........");
}
void search(void)
{
int num,i,flag=0,po;
printf("\nEnter the element for searching purpose:");
scanf("%d",&num);
for(i=0;i<n;i++)
{
if(a[i]==num)
{
po=i;
flag++;
}
}
if(flag==0)
printf("\nGiven number is not present in the Array");
else
{
printf("\nGiven number is present in the array with %d time(s)",flag);
printf("\nPosition is %d",po+1);
}
}
3

void maxmin(void)
{
int max=a[0],min=a[0],i=0;
while(i<n)
{
if(max<a[i])
max=a[i];
if(min>a[i])
min=a[i];
i++;
}
printf("\nMaximum element is %d",max);
printf("\nMinimum element is %d",min);
}
void ascending(void)
{
int i=0,j,b[100],temp;
for(i=0;i<n;i++)
b[i]=a[i];
i=0;
while(i<n)
{
j=i+1;
while(j<n)
{
if(b[i]>b[j])
{
temp=b[i];
b[i]=b[j];
b[j]=temp;
}
j++;
}
i+=1;
}
printf("\n\nAfter ascending.......\n");
for(i=0;i<n;i++)
printf("\n %d",b[i]);
}
void descending(void)
{
int i=0,j,b[100],temp;
for(i=0;i<n;i++)
b[i]=a[i];
i=0;
while(i<n)
4

{
j=i+1;
while(j<n)
{
if(b[i]<b[j])
{
temp=b[i];
b[i]=b[j];
b[j]=temp;
}
j++;
}
i+=1;
}
printf("\n\nAfter descending.......\n");
for(i=0;i<n;i++)
printf("\n %d",b[i]);
}
void display(void)
{
int i;
for(i=0;i<n;i++)
printf("\nElement of position %d is %d",i+1,a[i]);
}

OUTPUT:
suvranshu@suvranshu-desktop:~/Desktop/Student\$ gcc array.c -o array
suvranshu@suvranshu-desktop:~/Desktop/Student\$ ./array
Enter how many elements you want to enter:5
.....Enter the Array element one by one.....
Enter the element no 1:25
Enter the element no 2:45
Enter the element no 3:12
Enter the element no 4:96
Enter the element no 5:87
...The above Array is...
Element no 1 is 25
Element no 2 is 45
Element no 3 is 12
Element no 4 is 96
Element no 5 is 87
1 for INSERT an element
2 for DELETE an element
3 for Search an element
4 for Find Maximum and Minimum element
5

## 5 for Sort in Ascending order

6 for Sort in Descending Order
7 for display the Array
8 for EXIT
Enter the above option:1
Enter the Position of the array:12
You Enter wrong position.
........Try again........
1 for INSERT an element
2 for DELETE an element
3 for Search an element
4 for Find Maximum and Minimum element
5 for Sort in Ascending order
6 for Sort in Descending Order
7 for display the Array
8 for EXIT
Enter the above option:1
Enter the Position of the array:3
Enter the new element (number) of this position:212
212 is Inserted in the ARRAY
1 for INSERT an element
2 for DELETE an element
3 for Search an element
4 for Find Maximum and Minimum element
5 for Sort in Ascending order
6 for Sort in Descending Order
7 for display the Array
8 for EXIT
Enter the above option:1
Enter the Position of the array:5
Enter the new element (number) of this position:8
8 is Inserted in the ARRAY
1 for INSERT an element
2 for DELETE an element
6

## 3 for Search an element

4 for Find Maximum and Minimum element
5 for Sort in Ascending order
6 for Sort in Descending Order
7 for display the Array
8 for EXIT
Enter the above option:7
Element of position 1 is 25
Element of position 2 is 45
Element of position 3 is 212
Element of position 4 is 12
Element of position 5 is 8
Element of position 6 is 96
Element of position 7 is 87
1 for INSERT an element
2 for DELETE an element
3 for Search an element
4 for Find Maximum and Minimum element
5 for Sort in Ascending order
6 for Sort in Descending Order
7 for display the Array
8 for EXIT
Enter the above option:4
Maximum element is 212
Minimum element is 8
1 for INSERT an element
2 for DELETE an element
3 for Search an element
4 for Find Maximum and Minimum element
5 for Sort in Ascending order
6 for Sort in Descending Order
7 for display the Array
8 for EXIT
Enter the above option:3
Enter the element for searching purpose:99
Given number is not present in the Array
1 for INSERT an element
2 for DELETE an element
3 for Search an element
4 for Find Maximum and Minimum element
5 for Sort in Ascending order
7

## 6 for Sort in Descending Order

7 for display the Array
8 for EXIT
Enter the above option:3
Enter the element for searching purpose:45
Given number is present in the array with 1 time(s)
Position is 2
1 for INSERT an element
2 for DELETE an element
3 for Search an element
4 for Find Maximum and Minimum element
5 for Sort in Ascending order
6 for Sort in Descending Order
7 for display the Array
8 for EXIT
Enter the above option:5
After ascending.......
8
12
25
45
87
96
212
1 for INSERT an element
2 for DELETE an element
3 for Search an element
4 for Find Maximum and Minimum element
5 for Sort in Ascending order
6 for Sort in Descending Order
7 for display the Array
8 for EXIT
Enter the above option:7
Element of position 1 is 25
Element of position 2 is 45
Element of position 3 is 212
Element of position 4 is 12
Element of position 5 is 8
Element of position 6 is 96
Element of position 7 is 87
8

## 1 for INSERT an element

2 for DELETE an element
3 for Search an element
4 for Find Maximum and Minimum element
5 for Sort in Ascending order
6 for Sort in Descending Order
7 for display the Array
8 for EXIT
Enter the above option:6
After descending.......
212
96
87
45
25
12
8
1 for INSERT an element
2 for DELETE an element
3 for Search an element
4 for Find Maximum and Minimum element
5 for Sort in Ascending order
6 for Sort in Descending Order
7 for display the Array
8 for EXIT
Enter the above option:7
Element of position 1 is 25
Element of position 2 is 45
Element of position 3 is 212
Element of position 4 is 12
Element of position 5 is 8
Element of position 6 is 96
Element of position 7 is 87
1 for INSERT an element
2 for DELETE an element
3 for Search an element
4 for Find Maximum and Minimum element
5 for Sort in Ascending order
6 for Sort in Descending Order
7 for display the Array
8 for EXIT
Enter the above option:8
9

suvranshu@suvranshu-desktop:~/Desktop/Student\$

10

## Two Dimensional Array

1. Write a program in C, to create a matrix(two dimensional array) and display this matrix.
Matrix Order & Elements are given by user.
Ans:
#include<stdio.h>
int main(void)
{
int m[10][10],r,c,i,j;
printf("\nEnter no. of rows:");
scanf("%d",&r);
printf("\nEnter no. of collumns:");
scanf("%d",&c);
printf("\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("Enter the element for the position %d & %d: ",i+1,j+1);
scanf("%d",&m[i][j]);
}
}
printf("\n\n.....Given Matrx is.....\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d ",m[i][j]);
}
printf("\n");
}
return 0;
}

Output:
suvranshu@suvranshu-desktop:~/Desktop/Student\$ gcc matrix.c -o matrix
suvranshu@suvranshu-desktop:~/Desktop/Student\$ ./matrix
Enter no. of rows:3
Enter no. of collumns:4
Enter the element for the position 1 & 1: 15
Enter the element for the position 1 & 2: 25
Enter the element for the position 1 & 3: 87
Enter the element for the position 1 & 4: 45
Enter the element for the position 2 & 1: 36
Enter the element for the position 2 & 2: 95
Enter the element for the position 2 & 3: 10
Enter the element for the position 2 & 4: 58
Enter the element for the position 3 & 1: 69
Enter the element for the position 3 & 2: 36
Enter the element for the position 3 & 3: 89
11

## Enter the element for the position 3 & 4: 56

.....Given Matrx is.....
15 25 87 45
36 95 10 58
69 36 89 56
suvranshu@suvranshu-desktop:~/Desktop/Student\$
2. Write a program in C to find an element is present in matrix or not. Matrix Order &
Elements are given by user.
Ans:
#include<stdio.h>
int main(void)
{
int m[10][10],r,c,i,j,num,flag=0,por,poc;
printf("\nEnter no. of rows:");
scanf("%d",&r);
printf("\nEnter no. of collumns:");
scanf("%d",&c);
printf("\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("Enter the element for the position %d & %d: ",i+1,j+1);
scanf("%d",&m[i][j]);
}
}
printf("\n\n.....Given Matrx is.....\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d ",m[i][j]);
}
printf("\n");
}
printf("\nEnter the element for searching purpose:");
scanf("%d",&num);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(num==m[i][j])
{
flag++;
por=i;
poc=j;
}
}
}
12

if(flag==0)
printf("\nGiven number is not present in the Array");
else
{
printf("\nGiven number is present in the array with %d time(s)",flag);
printf("\nPosition is row:%d, col=%d",por+1,poc+1);
}
return 0;
}
3. Write a program in C to transpose a given matrix. Matrix Order & Elements are given by
user.
Ans:
#include<stdio.h>
int main(void)
{
int m[10][10],r,c,i,j;
printf("\nEnter no. of rows:");
scanf("%d",&r);
printf("\nEnter no. of collumns:");
scanf("%d",&c);
printf("\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("Enter the element for the position %d & %d: ",i+1,j+1);
scanf("%d",&m[i][j]);
}
}
printf("\n\n.....Given Matrx is.....\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d ",m[i][j]);
}
printf("\n");
}
printf("\n\n.....After transporation.....\n");
for(i=0;i<c;i++)
{
for(j=0;j<r;j++)
{
printf("%d ",m[j][i]);
}
printf("\n");
}
return 0;
}

13

OUTPUT:
suvranshu@suvranshu-desktop:~/Desktop/Student\$ gcc mtranspose.c -o trans
suvranshu@suvranshu-desktop:~/Desktop/Student\$ ./trans
Enter no. of rows:3
Enter no. of collumns:4
Enter the element for the position 1 & 1: 2
Enter the element for the position 1 & 2: 5
Enter the element for the position 1 & 3: 8
Enter the element for the position 1 & 4: 4
Enter the element for the position 2 & 1: 3
Enter the element for the position 2 & 2: 6
Enter the element for the position 2 & 3: 9
Enter the element for the position 2 & 4: 1
Enter the element for the position 3 & 1: 2
Enter the element for the position 3 & 2: 5
Enter the element for the position 3 & 3: 6
Enter the element for the position 3 & 4: 4
.....Given Matrx is.....
2 5 8 4
3 6 9 1
2 5 6 4
.....After transporation.....
2 3 2
5 6 5
8 9 6
4 1 4
suvranshu@suvranshu-desktop:~/Desktop/Student\$
4. Write a program in C to multiplication of two matrices. Result is store in another matrix.
Matrix Order & Elements are given by user.
Ans:
#include<stdio.h>
int main(void)
{
int a[20][20],b[20][20],c[20][20],r1,c1,r2,c2,i,j,k;
printf("\n.....Details of 1st matrix.....");
printf("\nEnter the No. of rows:");
scanf("%d",&r1);
printf("Enter the no. of column:");
scanf("%d",&c1);
printf("\n");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
{
printf("Enter the element for the position %d & %d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
14

## printf("\n.....Details of 2nd matrix.....");

printf("\nEnter the No. of rows:");
scanf("%d",&r2);
printf("Enter the no. of column:");
scanf("%d",&c2);
printf("\n");
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
{
printf("Enter the element for the position %d & %d: ",i+1,j+1);
scanf("%d",&b[i][j]);
}
printf("\n\n.....Given 1st Matrx is.....\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%d ",a[i][j]);
printf("\n");
}
printf("\n.....Given 2nd Matrx is.....\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
printf("%d ",b[i][j]);
printf("\n");
}
if(c1==r2)
{
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
c[i][j]=0;
for(k=0;k<r2;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("\n\n.....After Multiplication.....\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
printf("%d ",c[i][j]);
printf("\n");
}
}
else
printf("\n\nMatrix multiplication is NOT possible\n");
return 0;
}

15

OUTPUT:
suvranshu@suvranshu-desktop:~/Desktop/Student\$ gcc multiplication.c -o mul
suvranshu@suvranshu-desktop:~/Desktop/Student\$ ./mul
.....Details of 1st matrix.....
Enter the No. of rows:2
Enter the no. of column:3
Enter the element for the position 1 & 1: 4
Enter the element for the position 1 & 2: 2
Enter the element for the position 1 & 3: -1
Enter the element for the position 2 & 1: 3
Enter the element for the position 2 & 2: -7
Enter the element for the position 2 & 3: 1
.....Details of 2nd matrix.....
Enter the No. of rows:3
Enter the no. of column:2
Enter the element for the position 1 & 1: 2
Enter the element for the position 1 & 2: 3
Enter the element for the position 2 & 1: -3
Enter the element for the position 2 & 2: 0
Enter the element for the position 3 & 1: -1
Enter the element for the position 3 & 2: 5
.....Given 1st Matrx is.....
4 2 -1
3 -7 1
.....Given 2nd Matrx is.....
2 3
-3 0
-1 5
.....After Multiplication.....
3 7
26 14
suvranshu@suvranshu-desktop:~/Desktop/Student\$ ./mul
.....Details of 1st matrix.....
Enter the No. of rows:3
Enter the no. of column:3
Enter the element for the position 1 & 1: 1
Enter the element for the position 1 & 2: 2
Enter the element for the position 1 & 3: 3
Enter the element for the position 2 & 1: 4
Enter the element for the position 2 & 2: 5
Enter the element for the position 2 & 3: 6
Enter the element for the position 3 & 1: 7
Enter the element for the position 3 & 2: 8
16

## Enter the element for the position 3 & 3: 9

.....Details of 2nd matrix.....
Enter the No. of rows:2
Enter the no. of column:3
Enter the element for the position 1 & 1: 10
Enter the element for the position 1 & 2: 20
Enter the element for the position 1 & 3: 30
Enter the element for the position 2 & 1: 40
Enter the element for the position 2 & 2: 50
Enter the element for the position 2 & 3: 60
.....Given 1st Matrx is.....
1 2 3
4 5 6
7 8 9
.....Given 2nd Matrx is.....
10 20 30
40 50 60
Matrix multiplication is NOT possible
suvranshu@suvranshu-desktop:~/Desktop/Student\$

17

## Prepared by Suvranshu Hazra

STRING
1. Write a program in C, to find the length of a String.
Ans:

Using Array:
#include<stdio.h>
int main(void)
{
char n[25];
int l=0;
printf("\nEnter any String:");
gets(n); //scanf() function not taking Space
while(n[l]!=0)
{
l=l+1;
}
printf("\n\nThe string '%s' contains %d Characters\n",n,l);
return 0;
}

## Using Array with sub function:

#include<stdio.h>
int str_length(char []);
int main(void)
{
char n[25];
int l;
printf("\nEnter any String:");
gets(n); //scanf() function not taking Space
l=str_length(n);
printf("\n\nThe string '%s' contains %d Characters\n",n,l);
return 0;
}
int str_length(char s[])
{
int c=0;
while(s[c]!='\0')
{
c=c+1;
}
return c;
}

//(or) while(s[c]!=NULL)

18

Using Pointer:
#include<stdio.h>
int main(void)
{
char a[50];
char *p;
int len=0;
printf("\nEnter a string:");
gets(a);
p=a;
//(or)p=&a[0]
while(*p!='\0')
{
len++;
p++;
}
printf("\nLength of the '%s' string is %d\n",a,len);
return 0;
}

## Using pointer with sub function:

#include<stdio.h>
int str_len(char *);
int main(void)
{
char a[50];
int len;
printf("\nEnter a string:");
gets(a);
len=str_len(a);
printf("\nLength of the '%s' string is %d\n",a,len);
return 0;
}
int str_len(char *s)
{
int l=0;
while(*s!='\0')
{
l++;
s++;
}
return l;
}

19

OUTPUT:
suvranshu@suvranshu-desktop:~/Desktop/Student\$ gcc slen_arr.c -o slen_arr
suvranshu@suvranshu-desktop:~/Desktop/Student\$ ./slen_arr
Enter any String:Computer science
The string 'Computer science' contains 16 Characters
suvranshu@suvranshu-desktop:~/Desktop/Student\$ ./slen_arr
Enter any String:h.s.
The string 'h.s.' contains 4 Characters
suvranshu@suvranshu-desktop:~/Desktop/Student\$

20

## 2. Write a program in C to copy a string into another array.

Ans:

Using Array:
#include<stdio.h>
int main(void)
{
char s[25],t[25];
int l=0;
printf("\nEnter a String:");
gets(s);
while(s[l]!='\0')
{
t[l]=s[l];
l++;
}
t[l]='\0';
printf("\n\nSource String:%s",s);
printf("\nTarget string:%s\n",t);
return 0;
}

## Using array with sub function:

#include<stdio.h>
void str_cpy(char [],char []);
int main(void)
{
char s[25],t[25];
int l;
printf("\nEnter a String:");
gets(s);
str_cpy(t,s);
printf("\n\nSource String:%s",s);
printf("\nTarget string:%s\n",t);
return 0;
}
void str_cpy(char x[],char y[])
{
int i=0;
while(y[i]!='\0')
{
x[i]=y[i];
i++;
}
x[i]='\0';
}
21

Using pointer:
#include<stdio.h>
int main(void)
{
char s[20],t[20];
char *p1,*p2;
printf("\nEnter a String:");
gets(s);
p1=s;
//(or)p1=&a[0]
p2=t;
while(*p1!='\0')
{
*p2=*p1;
p1++;
p2++;
}
*p2='\0';
printf("\nSource string is %s",s);
printf("\nTarget string is %s\n",t);
return 0;
}

## Using pointer with sub function:

#include<stdio.h>
void str_cpy(char *,char *);
int main(void)
{
char s[20],t[20];
printf("\nEnter a String:");
gets(s);
str_cpy(t,s);
printf("\nSource string is %s",s);
printf("\nTarget string is %s\n",t);
return 0;
}
void str_cpy(char *x,char *y)
{
while(*y!='\0')
{
*x=*y;
x++;
y++;
}
*x='\0';
}

22

OUTPUT:
suvranshu@suvranshu-desktop:~/Desktop/Student\$ gcc scpy_arr.c -o scpy_arr
suvranshu@suvranshu-desktop:~/Desktop/Student\$ ./scpy_arr
Enter a String:Computer
Source String:Computer
Target string:Computer
suvranshu@suvranshu-desktop:~/Desktop/Student\$ ./scpy_arr
Enter a String:Learn
Source String:Learn
Target string:Learn
suvranshu@suvranshu-desktop:~/Desktop/Student\$
3. Write a program in C to concatenate two strings.
Ans:
Using Pointer:
#include<stdio.h>
int main(void)
{
char source[50],target[50];
char *p1,*p2;
printf("\nEnter a Source string:");
gets(source);
printf("\nEnter the Target string:");
gets(target);
p1=source;
p2=target;
while(*p2!='\0')
{
p2++;
}
while(*p1!='\0')
{
*p2=*p1;
p1++;
p2++;
}
*p2='\0';
printf("\nSource String is %s",source);
printf("\nTarget String is %s\n",target);
return 0;
}

23

## Using pointer with sub function:

#include<stdio.h>
void str_cat(char *,char *);
int main(void)
{
char source[50],target[50];
printf("\nEnter a Source string:");
gets(source);
printf("\nEnter the Target string:");
gets(target);
str_cat(target,source);
printf("\nSource String is %s",source);
printf("\nTarget String is %s\n",target);
}
void str_cat(char *t,char *s)
{
while(*t!='\0')
{
t++;
}
while(*s!='\0')
{
*t=*s;
t++;
s++;
}
*t='\0';
}
OUTPUT:
suvranshu@suvranshu-desktop:~/Desktop/Student\$ gcc scat.c -o scat
suvranshu@suvranshu-desktop:~/Desktop/Student\$ ./scat
Enter a Source string:Science
Enter the Target string:Computer
Source String is Science
Target String is ComputerScience
suvranshu@suvranshu-desktop:~/Desktop/Student\$

24

4. Write a program in C to convert a string upper to lower or lower to upper. (as user choice)
Ans:
#include<stdio.h>
#include<stdlib.h>
void upp_low(char *);
void low_upp(char *);
int main(void)
{
char a[200];
int i;
printf("Enter a String:");
gets(a);
while(1)
{
printf("\nPress 1 for transfer the string UPPER to LOWER\nPress 2 for transfer the
string LOWER to UPPER\nPress 3 for Exit");
printf("\n\nEnter the above option:");
scanf("%d",&i);
switch(i)
{
case 1:
upp_low(a);
printf("\n.....After Lower case transfer.....\nString is %s\n\n",a);
break;
case 2:
low_upp(a);
printf("\n.....After Upper case transfer.....\nString is %s\n\n",a);
break;
case 3:
exit(1);
default:
Try Again");
}
}
return 0;
}
void upp_low(char *s)
{
while(*s!='\0')
{
if((*s>=65)&&(*s<=90))
{
*s=*s+32;
}
s++;
}
*s='\0';
}
25

## void low_upp(char *s)

{
while(*s!='\0')
{
if((*s>=97)&&(*s<=122))
{
*s=*s-32;
}
s++;
}
*s='\0';
}
OUTPUT:
suvranshu@suvranshu-desktop:~/Desktop/Student\$ gcc scase.c -o scase
suvranshu@suvranshu-desktop:~/Desktop/Student\$ ./scase
Enter a String:ComputER SCIEnce
Press 1 for transfer the string UPPER to LOWER
Press 2 for transfer the string LOWER to UPPER
Press 3 for Exit
Enter the above option:1
.....After Lower case transfer.....
String is computer science
Press 1 for transfer the string UPPER to LOWER
Press 2 for transfer the string LOWER to UPPER
Press 3 for Exit
Enter the above option:2
.....After Upper case transfer.....
String is COMPUTER SCIENCE
Press 1 for transfer the string UPPER to LOWER
Press 2 for transfer the string LOWER to UPPER
Press 3 for Exit
Enter the above option:3
suvranshu@suvranshu-desktop:~/Desktop/Student\$

26

## 5. Write a program in C to convert 1st lower case then upper case.

Ans:

Using Pointer:
#include<stdio.h>
int main(void)
{
char a[200];
char *p;
printf(Enter a String:);
gets(a);
p=a;
while(*p!=\0)
{
if((*p>=65)&&(*p<=90))
{
*p=*p+32;
}
p++;
}
*p=\0;
printf(\n..After Lower case transfer..\nString is %s,a);
p=a;
while(*p!=\0)
{
if((*p>=97)&&(*p<=122))
{
*p=*p-32;
}
p++;
}
*p=\0;
printf(\n\n..After Upper case transfer..\nString is %s\n,a);
return 0;
}

## Using pointer with Sub function:

#include<stdio.h>
void upp_low(char *);
void low_upp(char *);
int main(void)
{
char a[200];
printf("Enter a String:");
gets(a);
upp_low(a);
27

## printf("\n.....After Lower case transfer.....\nString is %s",a);

low_upp(a);
printf("\n\n.....After Upper case transfer.....\nString is %s\n",a);
return 0;
}
void upp_low(char *s)
{
while(*s!='\0')
{
if((*s>=65)&&(*s<=90))
{
*s=*s+32;
}
s++;
}
*s='\0';
}
void low_upp(char *s)
{
while(*s!='\0')
{
if((*s>=97)&&(*s<=122))
{
*s=*s-32;
}
s++;
}
*s='\0';
}

OUTPUT:
suvranshu@suvranshu-desktop:~/Desktop/Student\$ gcc scase1.c -o scase1
suvranshu@suvranshu-desktop:~/Desktop/Student\$ ./scase1
Enter a String:Computer Science
.....After Lower case transfer.....
String is computer science
.....After Upper case transfer.....
String is COMPUTER SCIENCE
suvranshu@suvranshu-desktop:~/Desktop/Student\$

28

## 6. Write a program in C to compare two strings.

Ans:
Using pointer:
#include<stdio.h>
#include<string.h>
int main(void)
{
char a[100],b[100];
char *p1,*p2;
int l1,l2,i=0,d;
printf("Enter the 1st string:");
gets(a);
printf("Enter the 2 nd String:");
gets(b);
p1=a;
p2=b;
l1=strlen(a);
l2=strlen(b);
if(l1<l2)
{
l1=l1+l2;
l2=l1-l2;
l1=l1-l2;
}
i=0;
while(i<l1)
{
if(*p1!=*p2)
{
d=*p1-*p2;
break;
}
p1++;
p2++;
i+=1;
}
printf("\nDifference between above two string\n'%s'-'%s'=%d\n",a,b,d);
return 0;
}

29

## Using pointer with sub function:

#include<stdio.h>
int str_cmp(char *,char *);
int strlen(char *);
int main(void)
{
char a[100],b[100];
int d;
printf("Enter the 1st string:");
gets(a);
printf("Enter the 2 nd String:");
gets(b);
d=str_cmp(a,b);
printf("\nDifference between above two string\n'%s'-'%s'=%d\n",a,b,d);
return 0;
}
int str_cmp(char *x,char *y)
{
int s=0,l1,l2,i=0;
l1=str_len(x);
l2=str_len(y);
if(l1<l2)
{
l1=l1+l2;
l2=l1-l2;
l1=l1-l2;
}
while(i<l1)
{
if(*x!=*y)
{
s=*x-*y;
break;
}
x++;
y++;
i+=1;
}
return s;
}
int str_len(char *s)
{
int l=0;
while(*s!='\0')
{
l++;
s++;
}
return l;
}
30

OUTPUT:
suvranshu@suvranshu-desktop:~/Desktop/Student\$ gcc scmp.c -o scmp
suvranshu@suvranshu-desktop:~/Desktop/Student\$ ./scmp
Enter the 1st string:Jerry
Enter the 2 nd String:Jerry
Difference between above two string
'Jerry'-'Jerry'=0
suvranshu@suvranshu-desktop:~/Desktop/Student\$ ./scmp
Enter the 1st string:Jerry
Enter the 2 nd String:Ferry
Difference between above two string
'Jerry'-'Ferry'=4
suvranshu@suvranshu-desktop:~/Desktop/Student\$ ./scmp
Enter the 1st string:Jerry
Enter the 2 nd String:Jerry Boy
Difference between above two string
'Jerry'-'Jerry Boy'=-32
suvranshu@suvranshu-desktop:~/Desktop/Student\$ ./scmp
Enter the 1st string:JERRY
Enter the 2 nd String:JeRry
Difference between above two string
'JERRY'-'JeRry'=-32
suvranshu@suvranshu-desktop:~/Desktop/Student\$
7. Write a program in C to print a string in reverse order. (Ex. INDIA AIDNI)
Ans:
Using Pointer:
#include<stdio.h>
#include<string.h>
int main(void)
{
char a[200],b[100];
char *p1,*p2;
int l,i=0;
printf("Enter a String:");
gets(a);
p1=a;
p2=b;
l=strlen(a);
p1=p1+(l-1);
i=0;
while(i<l)
{
*p2=*p1;
p2++;
p1--;
i+=1;
31

}
*p2='\0'
;
printf("\n.....After Reverse.....");
printf("\nOriginal String %s",a);
printf("\nReverse String %s\n",b);
return 0;
}
Using pointer with sub function:
#include<stdio.h>
void reverse(char *,char *);
int str_len(char *);
int main(void)
{
char a[200],b[100];
printf("Enter a String:");
gets(a);
reverse(a,b);
printf("\n.....After Swap.....");
printf("\nOriginal String %s",a);
printf("\nReverse String %s\n",b);
return 0;
}
void reverse(char *x,char *y)
{
int l,i=0;
l=str_len(x);
x=x+(l-1);
while(i<l)
{
*y=*x;
y++;
x--;
i+=1;
}
*y='\0';
}
int str_len(char *s)
{
int l=0;
while(*s!='\0')
{
l++;
s++;
}
return l;
}

32

## 7. Write a program in C to print a string is palindrome or not.

Ans:

Using array:
#include<stdio.h>
#include<string.h>
int main(void)
{
char a[200],b[100];
int l,i,d,k=0;
printf("Enter a String:");
gets(a);
l=strlen(a);
k=0;
for(i=l-1;i>=0;i--)
{
b[k]=a[i];
k++;
}
b[k]='\0';
printf("\n.....After Reverse.....");
printf("\nOriginal String %s",a);
printf("\nReverse String %s\n",b);
d=strcmp(a,b);
if(d==0)
printf("\nThe string '%s' is palindrom\n",a);
else
printf("\nThe string '%s' is NOT palindrom\n",a);
return 0;
}

Using Pointer:
#include<stdio.h>
#include<string.h>
int main(void)
{
char a[200],b[100];
char *p1,*p2;
int l,i=0,d;
printf("Enter a String:");
gets(a);
p1=a;
p2=b;
l=strlen(a);
p1=p1+(l-1);
i=0;
while(i<l)
{
*p2=*p1;
p2++;
p1--;
i+=1;
33

}
*p2='\0';
printf("\n.....After Reverse.....");
printf("\nOriginal String %s",a);
printf("\nReverse String %s\n",b);
d=str_cmp(a,b);
if(d==0)
printf("\nThe string '%s' is palindrom\n",a);
else
printf("\nThe string '%s' is NOT palindrom\n",a);
return 0;
}

## Using Pointer with sub function:

#include<stdio.h>
void palindrom(char *,char *);
int str_len(char *);
int str_cmp(char *,char *);
int main(void)
{
char a[200],b[100];
int d;
printf("Enter a String:");
gets(a);
palindrom(a,b);
printf("\n.....After Swap.....");
printf("\nOriginal String %s",a);
printf("\nReverse String %s\n",b);
d=str_cmp(a,b);
if(d==0)
printf("\nThe string '%s' is palindrom\n",a);
else
printf("\nThe string '%s' is NOT palindrom\n",a);
return 0;
}
void palindrom(char *x,char *y)
{
int l,i=0;
l=str_len(x);
x=x+(l-1);
while(i<l)
{
*y=*x;
y++;
x--;
i+=1;
}
*y='\0';
}

34

## int str_len(char *s)

{
int l=0;
while(*s!='\0')
{
l++;
s++;
}
return l;
}
int str_cmp(char *x,char *y)
{
int s=0,l1,l2,i=0;
l1=str_len(x);
l2=str_len(y);
if(l1<l2)
{
l1=l1+l2;
l2=l1-l2;
l1=l1-l2;
}
while(i<l1)
{
if(*x!=*y)
{
s=*x-*y;
break;
}
x++;
y++;
i+=1;
}
return s;
}

35

## Prepared by Suvranshu Hazra

Prepared by Suvranshu Hazra

STACK
1. Write a program in C to accept a stack (using Array) and perform the following operation:
i. PUSH an Element,
ii. POP an Element,
iii. Display the Stack.
All operation should be in separate function. Size of stack will be user defined. The entire system
Ans:
#include<stdio.h>
int a[100],n,top=-1;
void push(void);
void pop(void);
void display(void);
int main(void)
{
int i;
printf("\nEnter the Size of of the STACK:");
scanf("%d",&n);
do
{
printf("\n\n\n1 for PUSH\n2 for POP\n3 for DISPLAY\n4 for EXIT");
printf("\n\nPress the key of the above option:");
scanf("%d",&i);
switch(i)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
break;
default:
Try Again");
}
}while(i!=4);
return 0;
}
36

void push(void)
{
int i;
if(top==(n-1))
printf("\n\n\t***The Stack is OVER FLOWED***\n\t No element can be PUSH");
else
{
top++;
printf("\nEnter the number:");
scanf("%d",&i);
a[top]=i;
printf("\n\n%d is PUSH in the Stack",i);
}
}
void pop(void)
{
if(top==-1)
printf("\n\n\t***The Stack is UNDER FLOWED***\n\t So No element can be POP");
else
{
printf("\n\n%d is POP from the Stack",a[top]);
top=top-1;
}
}
void display(void)
{
int i;
if(top==-1)
printf("\n\n\t***The Stack is UNDERFLOWED***");
else
{
printf("\n|--------|");
for(i=top;i>=0;i--)
{
printf("\n %d",a[i]);
printf("\n|--------|");
}
}
}

OUTPUT:
suvranshu@suvranshu-desktop:~/Desktop/Student\$ gcc stack.c -o stack
suvranshu@suvranshu-desktop:~/Desktop/Student\$ ./stack
Enter the Size of of the STACK:4

1 for PUSH
2 for POP
3 for DISPLAY
4 for EXIT
37

## Press the key of the above option:2

***The Stack is UNDER FLOWED***
So No element can be POP
1 for PUSH
2 for POP
3 for DISPLAY
4 for EXIT
Press the key of the above option:3
***The Stack is UNDERFLOWED***
1 for PUSH
2 for POP
3 for DISPLAY
4 for EXIT
Press the key of the above option:1
Enter the number:36
36 is PUSH in the Stack
1 for PUSH
2 for POP
3 for DISPLAY
4 for EXIT
Press the key of the above option:1
Enter the number:10
10 is PUSH in the Stack
1 for PUSH
2 for POP
3 for DISPLAY
4 for EXIT
Press the key of the above option:3
|--------|
10
38

|--------|
36
|--------|
1 for PUSH
2 for POP
3 for DISPLAY
4 for EXIT
Press the key of the above option:1
Enter the number:87
87 is PUSH in the Stack
1 for PUSH
2 for POP
3 for DISPLAY
4 for EXIT
Press the key of the above option:1
Enter the number:95
95 is PUSH in the Stack
1 for PUSH
2 for POP
3 for DISPLAY
4 for EXIT
Press the key of the above option:1
***The Stack is OVER FLOWED***
No element can be PUSH
1 for PUSH
2 for POP
3 for DISPLAY
4 for EXIT
Press the key of the above option:3
|--------|
95
|--------|
87
39

|--------|
10
|--------|
36
|--------|
1 for PUSH
2 for POP
3 for DISPLAY
4 for EXIT
Press the key of the above option:2
95 is POP from the Stack
1 for PUSH
2 for POP
3 for DISPLAY
4 for EXIT
Press the key of the above option:3
|--------|
87
|--------|
10
|--------|
36
|--------|
1 for PUSH
2 for POP
3 for DISPLAY
4 for EXIT
Press the key of the above option:2
87 is POP from the Stack
1 for PUSH
2 for POP
3 for DISPLAY
4 for EXIT
Press the key of the above option:2
10 is POP from the Stack
40

1 for PUSH
2 for POP
3 for DISPLAY
4 for EXIT
Press the key of the above option:3
|--------|
36
|--------|
1 for PUSH
2 for POP
3 for DISPLAY
4 for EXIT
Press the key of the above option:2
36 is POP from the Stack
1 for PUSH
2 for POP
3 for DISPLAY
4 for EXIT
Press the key of the above option:2
***The Stack is UNDER FLOWED***
So No element can be POP
1 for PUSH
2 for POP
3 for DISPLAY
4 for EXIT
Press the key of the above option:4
suvranshu@suvranshu-desktop:~/Desktop/Student\$

41

## Prepared by Suvranshu Hazra

QUEUE
1. Write a program in C to accept a Queue (using Array) and perform the following operation:
a. Insertion,
b. Deletion,
c. Display
All operation should be in separate function. Size of Queue will be user defined. The entire
Ans:
#include<stdio.h>
int q[100],n,front=-1,rear=-1;
void qinsert(void);
void qdelete(void);
void qdisplay(void);
int main(void)
{
int i;
printf("\nEnter the Size of of the QUEUE:");
scanf("%d",&n);
do
{
printf("\n\n\n1 for Insert\n2 for DELETE\n3 for DISPLAY\n4 for EXIT");
printf("\n\nPress the key of the above option:");
scanf("%d",&i);
switch(i)
{
case 1:
qinsert();
break;
case 2:
qdelete();
break;
case 3:
qdisplay();
break;
case 4:
break;
default:
Try Again");
}
}while(i!=4);
return 0;
}

42

void qinsert(void)
{
int item;
if((front==0&&rear==(n-1))||(front==rear+1))
printf("\n\n\t***The Queue is Full***\n\t No element can be iserted");
else
{
if(front==-1)
{
front=0;
rear=0;
}
else if(rear==n-1)
rear=0;
else
rear=rear+1;
printf("\nEnter the value:");
scanf("%d",&item);
q[rear]=item;
printf("\n%d is inserted",item);
}
}
void qdelete(void)
{
int item;
if(front==-1)
printf("\n\n\t***The Queue is Empty***\n\t So No element can be deleted");
else
{
item=q[front];
if(front==rear)
{
front=-1;
rear=-1;
}
else if(front==n-1)
front=0;
else
front=front+1;
printf("\n%d is deleted",item);
}
}
void qdisplay(void)
{
int i;
if(rear==-1)
printf("\n\n\t***The Queue is Empty***");
else
{
if(rear>=front)
for(i=front;i<=rear;i++)
printf("%d ",q[i]);
43

else
{
for(i=front;i<=n-1;i++)
printf("%d ",q[i]);
for(i=0;i<=rear;i++)
printf("%d ",q[i]);
}
}
}

OUTPUT:
suvranshu@suvranshu-desktop:~/Desktop/Student\$ gcc queue.c -o queue
suvranshu@suvranshu-desktop:~/Desktop/Student\$ ./queue
Enter the Size of of the QUEUE:5

1 for Insert
2 for DELETE
3 for DISPLAY
4 for EXIT
Press the key of the above option:2
***The Queue is Empty***
So No element can be deleted
1 for Insert
2 for DELETE
3 for DISPLAY
4 for EXIT
Press the key of the above option:3
***The Queue is Empty***
1 for Insert
2 for DELETE
3 for DISPLAY
4 for EXIT
Press the key of the above option:1
Enter the value:10
10 is inserted
1 for Insert
44

2 for DELETE
3 for DISPLAY
4 for EXIT
Press the key of the above option:1
Enter the value:20
20 is inserted
1 for Insert
2 for DELETE
3 for DISPLAY
4 for EXIT
Press the key of the above option:1
Enter the value:30
30 is inserted
1 for Insert
2 for DELETE
3 for DISPLAY
4 for EXIT
Press the key of the above option:3
10 20 30
1 for Insert
2 for DELETE
3 for DISPLAY
4 for EXIT
Press the key of the above option:2
10 is deleted
1 for Insert
2 for DELETE
3 for DISPLAY
4 for EXIT
Press the key of the above option:3
20 30
1 for Insert
2 for DELETE
3 for DISPLAY
45

4 for EXIT
Press the key of the above option:1
Enter the value:40
40 is inserted
1 for Insert
2 for DELETE
3 for DISPLAY
4 for EXIT
Press the key of the above option:1
Enter the value:50
50 is inserted
1 for Insert
2 for DELETE
3 for DISPLAY
4 for EXIT
Press the key of the above option:1
Enter the value:60
60 is inserted
1 for Insert
2 for DELETE
3 for DISPLAY
4 for EXIT
Press the key of the above option:1
***The Queue is Full***
No element can be iserted
1 for Insert
2 for DELETE
3 for DISPLAY
4 for EXIT
Press the key of the above option:2
20 is deleted
46

1 for Insert
2 for DELETE
3 for DISPLAY
4 for EXIT
Press the key of the above option:3
30 40 50 60
1 for Insert
2 for DELETE
3 for DISPLAY
4 for EXIT
Press the key of the above option:2
30 is deleted
1 for Insert
2 for DELETE
3 for DISPLAY
4 for EXIT
Press the key of the above option:2
40 is deleted
1 for Insert
2 for DELETE
3 for DISPLAY
4 for EXIT
Press the key of the above option:3
50 60
1 for Insert
2 for DELETE
3 for DISPLAY
4 for EXIT
Press the key of the above option:1
Enter the value:70
70 is inserted
1 for Insert
2 for DELETE
3 for DISPLAY
47

4 for EXIT
Press the key of the above option:1
Enter the value:80
80 is inserted
1 for Insert
2 for DELETE
3 for DISPLAY
4 for EXIT
Press the key of the above option:3
50 60 70 80
1 for Insert
2 for DELETE
3 for DISPLAY
4 for EXIT
Press the key of the above option:2
50 is deleted
1 for Insert
2 for DELETE
3 for DISPLAY
4 for EXIT
Press the key of the above option:2
60 is deleted
1 for Insert
2 for DELETE
3 for DISPLAY
4 for EXIT
Press the key of the above option:2
70 is deleted
1 for Insert
2 for DELETE
3 for DISPLAY
4 for EXIT
Press the key of the above option:3
48

80
1 for Insert
2 for DELETE
3 for DISPLAY
4 for EXIT
Press the key of the above option:2
80 is deleted
1 for Insert
2 for DELETE
3 for DISPLAY
4 for EXIT
Press the key of the above option:3
***The Queue is Empty***
1 for Insert
2 for DELETE
3 for DISPLAY
4 for EXIT
Press the key of the above option:2
***The Queue is Empty***
So No element can be deleted
1 for Insert
2 for DELETE
3 for DISPLAY
4 for EXIT
Press the key of the above option:4
suvranshu@suvranshu-desktop:~/Desktop/Student\$

49