Assignment -01
1. Write a C program to concatenate two string using pointer
Source Code:
#include <stdio.h>
#include <string.h>
void con_string(char *,char *);
int main()
{
char a[20],b[20];
printf("\nEnter string: ");
gets(a);
printf("\nEnter string: ");
gets(b);
con_string(a, b);
printf("\n After concatenation strings are : %s",a);
}
void con_string(char *x,char *y)
{
while(*x!='\0')
x++;
while(*y!='\0')
{
*x=*y;
y++;
x++;
}
*x='\0';
Output :
2.Write a C program for multiplication of two sparse matrices .
Source Code:
#include <stdio.h>
int main()
{
int r1, c1, r2, c2, c, d, k, sum = 0;
int first[10][10], second[10][10], result[10][10];
printf("Enter number of rows and columns of first matrix\n");
scanf("%d%d", &r1, &c1);
printf("Enter elements of first matrix\n");
for (c = 0; c < r1; c++)
for (d = 0; d < c1; d++){
if(c==d)
first[c][d]=0;
else
scanf("%d", &first[c][d]);
}
printf("Enter number of rows and columns of second matrix\n");
scanf("%d%d", &r2, &c2);
if (c1 != r2)
printf("The matrices can't be multiplied with each other.\n");
else
{
printf("Enter elements of second matrix\n");
for (c = 0; c < r2; c++)
for (d = 0; d < c2; d++){
if(c==d)
second[c][d]=0;
else
scanf("%d", &second[c][d]);
}
printf("\n 1st Matrix :\n");
for (c = 0; c < r1; c++)
{
for (d = 0; d < c1; d++)
printf("%4d",first[c][d]);
printf("\n");
}
printf("\n 2st Matrix :\n");
for (c = 0; c < r2; c++)
{
for (d = 0; d < c2; d++)
printf("%4d",second[c][d]);
printf("\n");
}
for (c = 0; c < r1; c++) {
for (d = 0; d < c2; d++) {
for (k = 0; k < r1; k++) {
sum = sum + first[c][k]*second[k][d];
}
result[c][d] = sum;
sum = 0;
}
}
printf("Product of the matrices:\n");
for (c = 0; c < r1; c++) {
for (d = 0; d < c2; d++)
printf("%d\t", result[c][d]);
printf("\n");
}
}
return 0;
}
Output :
3.Write a C program to dynamically creating a two dimensional array.
Source Code:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int **p;
int i,j,r,c;
printf("enter the no of row and coloumn ");
scanf("%d%d",&r,&c);
printf("\n Enter data ");
p=(int **)malloc(sizeof(int*)*r);
for(i=0;i<r;i++)
{
p[i]=(int*)malloc(sizeof(int)*c);
for(j=0;j<c;j++)
scanf("%d",&p[i][j]);
}
printf("\nThe %dX%d Matrix is :\n\n",r,c);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
printf(" %4d",p[i][j]);
printf("\n");
}
Output :
4.Write a C program to count the number of occurrences of each digit, of white
space characters (blank, tab, newline), and of all other characters.
Source Code:
#include <stdio.h>
int main()
{
char str[30];
int alphabets, digits, space,tab,ch,i;
alphabets=digits=space=tab=ch=0;
printf("Enter any string : ");
gets(str);
i=0;
while(str[i]!='\0')
{
if((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z'))
{
alphabets++;
}
else if(str[i]>='0' && str[i]<='9')
{
digits++;
}
else if(str[i]=='\t')
{
tab++;
}
else if(str[i]==' ')
{
space++;
}
else
{
ch++;
}
i++;
}
printf("Alphabets = %d\n", alphabets);
printf("Digits = %d\n", digits);
printf("Spaces = %d\n",space);
printf("Tabs = %d\n",tab);
printf("Special characters = %d\n", ch);
return 0;
}
Output :
ASSIGNMENT 02
1. Write a C program to read 10 elements and sort the array using insertion sort.
Source Code
#include<stdio.h>
void insertionsort(int n,int a[30])
{
int t,i,j;
for(i=1;i<n;i++)
{
t=a[i];
for(j=i-1;j>=0;j--)
{
if(t<a[j])
{
a[j+1]=a[j];
}
else
break;
}
a[j+1]=t;
}
}
int main()
{
int a[30],i,j,n;
printf("\n Enter no of elements");
scanf("%d",&n);
printf("\n Enter data");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
insertionsort(n,a);
for(i=0;i<n;i++)
printf("\nAfter sorting elements are %d:",a[i]);
Output
Enter no of elements5
Enter data3
0
2
6
9
After sorting elements are : 0
After sorting elements are : 2
After sorting elements are : 3
After sorting elements are : 6
After sorting elements are : 9
--------------------------------
Process exited after 3.615 seconds with return value 5
Press any key to continue . . .
2. Write a C program to read 10 elements and sort the above numbers using
bubble sort.
Source Code
#include<stdio.h>
void bubblesort(int n,int a[30])
{
int t,i,j;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
}
int main()
{
int a[30],i,j,n;
printf("\n Enter no of elements");
scanf("%d",&n);
printf("\n Enter data");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
bubblesort(n,a);
for(i=0;i<n;i++)
printf("\nAfter sorting elements are :%d",a[i]);
Output
Enter no of elements5
Enter data2
3
0
6
9
After sorting elements are :0
After sorting elements are :2
After sorting elements are :3
After sorting elements are :6
After sorting elements are :9
--------------------------------
Process exited after 5.304 seconds with return value 5
Press any key to continue . . .
3. Write a C program to read 10 elements and sort the above numbers using
quick sort.
Source Code
#include<stdio.h>
void quicksort(int x[20],int first,int last)
{
int pivot,j,t,i;
if(first<last)
{
pivot=first;
i=first;
j=last;
while(i<j)
{
while(x[i]<=x[pivot]&&i<last)
i++;
while(x[j]>x[pivot])
j--;
if(i<j)
{
t=x[i];
x[i]=x[j];
x[j]=t;
}
}
t=x[pivot];
x[pivot]=x[j];
x[j]=t;
quicksort(x,first,j-1);
quicksort(x,j+1,last);
}
}
int main()
{
int x[20],n,i;
printf(" Enter no of elements: ");
scanf("%d",&n);
printf("\n Enterelements: \n");
for(i=0;i<n;i++)
scanf("%d",&x[i]);
quicksort(x,0,n-1);
printf("\n Sorted elements after applying quick sort: \n\n");
for(i=0;i<n;i++)
printf(" %d",x[i]);
return 0;
}
Output
Enter no of elements: 6
Enterelements:
3
0
2
1
54
4
Sorted elements after applying quick sort:
0 1 2 3 4 54
--------------------------------
Process exited after 7.137 seconds with return value 0
Press any key to continue . . .
4. Write a C program to Sorting of a structure on names using bubble sort.
Source Code
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void **bubblesortstring(char **p,int n)
{
int x,i,j;
char t[50];
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
x=strcmp(p[j],p[j+1]);
if(x>0)
{
strcpy(t,p[j]);
strcpy(p[j],p[j+1]);
strcpy(p[j+1],t);
}
}
}
int main()
{
char **p;
int i,n;
printf("\nEnter no of names :");
scanf("%d",&n);
p=(char**)malloc(sizeof(char*)*n);
for(i=0;i<n;i++)
{
p[i]=(char *)malloc(30);
fflush(stdin);
printf("\n Enter Names");
gets(p[i]);
}
bubblesortstring(p,n);
for(i=0;i<n;i++)
printf("\n %s \n",p[i]);
}
Output
Enter no of names :5
Enter Names subha
Enter Names kousik
Enter Names avik
Enter Names sourav
Enter Names manish
avik
kousik
manish
sourav
subha
--------------------------------
Process exited after 12.72 seconds with return value 5
Press any key to continue . . .
ASSIGNMENT 03
1. Write a menu driven C program that implements all basic operations of a singly
linked list.
SOURCE CODE
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<malloc.h>
#define NULL0
typedef struct node
{
int data;
struct node *next;
}node;
node *getnode();
node *createlist(node *h);
node * displaylist(node *h);
node *insertbeg(node *h);
node *insertend(node *h);
node *insertmid(node *h);
node *delbeg(node *h);
node *delend(node *h);
node *delmid(node *h);
node *reverselist(node *h,node *p);
int main()
{
node *head=0,*p;
int p=NULL;
int x;
do
{
printf("\n1.Create List\n2.Display List\n3.Insert At Bigging\n4.Insert At End\n5.Insert
At Middle\n6.Delect From Begging \n7Delect From End\n8.Delect From Middle\n9.Exit From
Programme");
printf("\nEnter your choice : ");
scanf("%d",&x);
switch(x)
{
case 1:
head=createlist(head);
break;
case 2:
displaylist(head);
break;
case 3:
head=insertbeg(head);
break;
case 4:
head=insertend(head);
break;
case 5:
head =insertmid(head);
break;
case 6:
head=delbeg(head);
break;
case 7:
head=delend(head);
break;
case 8:
head=delmid(head);
break;
case 9:
exit (0);
default:
printf("Wrong Input");
printf("\nPlease Give A correct Input");
getch();
exit(0);
}
}while(1);
}
/*----------------------------------------GETNODE()------------------------------------------*/
node*getnode()
{
node *p;
int x;
p=(node *)malloc(sizeof (node));
printf("ENTER DATA \n");
scanf("%d",&x);
p->data=x;
p->next=NULL;
return p;
}
/*--------------------------------------CREATELIST()--------------------------------------------*/
node * createlist(node *h)
{
node *p,*s=NULL;
char ch='y';
h=NULL;
do
{
p=getnode();
if(h==NULL)
h=p;
else
s->next=p;
s=p;
printf("do you want to continue(y/n)");
fflush(stdin);
scanf("%c",&ch);
}while(ch=='y'||ch=='Y');
return h;
}
/*---------------------------------------DISPLAY LIST--------------------------*/
node *displaylist(node*h)
{
node *p;
p=h;
if(h==NULL)
{
printf("LIST NOT EXISTS");
getch();
return h;
}
p=h;
while(p!=NULL)
{
printf("%d\n",p->data);
p=p->next;
}
}
/*----------------------------------INSERT AT BEGINING-------------------------------*/
node * insertbeg(node * h)
{
node *p;
if(h==NULL)
{
printf("list not exists");
getch();
return h;
}
else
{
p=getnode();
p->next=h;
h=p;
}
return h;
}
/*-------------------------------------INSERT AT END----------------------------------*/
node* insertend(node* h)
{
node *p,*q;
if(h==NULL)
{
printf("list not exists");
getch();
return h;
}
else
{
p=getnode();
q=h;
while(q->next!= NULL)
q=q->next;
q->next=p;
}
return h;
}
/*---------------------------------------INDERT AT MIDDLE-----------------------------------*/
node * insertmid(node* h)
{
node *p,*q;
int c=1,pos;
if(h==NULL)
{
printf("list not exists");
getch();
return h;
}
p=getnode();
printf("\nenter position to insert");
scanf("%d",&pos);
q=h;
while(c<pos-1 && q->next!=NULL)
{
c++;
q=q->next;
}
if(pos==1)
{
p->next=q->next;
h=p;
}
else
{
p->next=q->next;
q->next=p;
}
return h;
}
/*----------------------------DELECT FROM BEGINING----------------------------*/
node *delbeg(node *h)
{
node *p;
if(h==NULL)
{
printf("\nlist not exists");
getch();
return h;
}
p=h;
h=h->next;
free(p);
return h;
}
/*------------------------------------------DELECT FROM END--------------------------*/
node* delend(node* h)
{
node* p,*q;
if(h==NULL)
{
printf("list not exists");
getch();
return h;
}
p=h;
q=p;
while(p->next!=NULL)
{
q=p;
p=p->next;
}
if(p==q)
h=NULL;
else
q->next=NULL;
free(p);
return h;
}
/*---------------------------DELECT FROM MIDDLE------------------------*/
node * delmid(node *h)
{
node *p,*q;
int x;
if(h==NULL)
{
printf("list not exists");
getch();
return h;
}
printf("Enter element to delect");
scanf("%d",&x);
p=q=h;
while(p->data!=x && p->next!=NULL)
{
q=p;
p=p->next;
}
if(p->data!=x)
{
printf("Element not found");
getch();
return h;
}
if(p==q)
h=h->next;
else
q->next=p->next;
free(p);
return h;
OUTPUT
1.Create List
2.Display List
3.Insert At Bigging
4.Insert At End
5.Insert At Middle
6.Delect From Begging
7Delect From End
8.Delect From Middle
9.Exit From Programme
Enter your choice : 1
ENTER DATA
2
do you want to continue(y/n)y
ENTER DATA
3
do you want to continue(y/n)n
1.Create List
2.Display List
3.Insert At Bigging
4.Insert At End
5.Insert At Middle
6.Delect From Begging
7Delect From End
8.Delect From Middle
9.Exit From Programme
Enter your choice : 2
2
3
1.Create List
2.Display List
3.Insert At Bigging
4.Insert At End
5.Insert At Middle
6.Delect From Begging
7Delect From End
8.Delect From Middle
9.Exit From Programme
Enter your choice : 3
ENTER DATA
5
1.Create List
2.Display List
3.Insert At Bigging
4.Insert At End
5.Insert At Middle
6.Delect From Begging
7Delect From End
8.Delect From Middle
9.Exit From Programme
Enter your choice : 4
ENTER DATA
9
1.Create List
2.Display List
3.Insert At Bigging
4.Insert At End
5.Insert At Middle
6.Delect From Begging
7Delect From End
8.Delect From Middle
9.Exit From Programme
Enter your choice : 5
ENTER DATA
0
enter position to insert2
1.Create List
2.Display List
3.Insert At Bigging
4.Insert At End
5.Insert At Middle
6.Delect From Begging
7Delect From End
8.Delect From Middle
9.Exit From Programme
Enter your choice : 2
5
0
2
3
9
1.Create List
2.Display List
3.Insert At Bigging
4.Insert At End
5.Insert At Middle
6.Delect From Begging
7Delect From End
8.Delect From Middle
9.Exit From Programme
Enter your choice : 6
1.Create List
2.Display List
3.Insert At Bigging
4.Insert At End
5.Insert At Middle
6.Delect From Begging
7Delect From End
8.Delect From Middle
9.Exit From Programme
Enter your choice : 2
0
2
3
9
1.Create List
2.Display List
3.Insert At Bigging
4.Insert At End
5.Insert At Middle
6.Delect From Begging
7Delect From End
8.Delect From Middle
9.Exit From Programme
Enter your choice : 7
1.Create List
2.Display List
3.Insert At Bigging
4.Insert At End
5.Insert At Middle
6.Delect From Begging
7Delect From End
8.Delect From Middle
9.Exit From Programme
Enter your choice : 2
0
2
3
1.Create List
2.Display List
3.Insert At Bigging
4.Insert At End
5.Insert At Middle
6.Delect From Begging
7Delect From End
8.Delect From Middle
9.Exit From Programme
Enter your choice : 8
Enter element to delect2
1.Create List
2.Display List
3.Insert At Bigging
4.Insert At End
5.Insert At Middle
6.Delect From Begging
7Delect From End
8.Delect From Middle
9.Exit From Programme
Enter your choice : 2
0
3
1.Create List
2.Display List
3.Insert At Bigging
4.Insert At End
5.Insert At Middle
6.Delect From Begging
7Delect From End
8.Delect From Middle
9.Exit From Programme
Enter your choice : 9
--------------------------------
Process exited after 38.59 seconds with return value 0
Press any key to continue . . .
2. Write a menu driven C program that implements all basic operations of a doubly
linked list
SOURCE CODE
#include<stdio.h>
#include<stdlib.h>
struct linkedlist {
struct linkedlist *prev;
int data;
struct linkedlist *next;
} *node = NULL, *first = NULL, *last = NULL, *node1 = NULL, *node2 = NULL;
void insert_beginning()
{
node = (struct linkedlist*)malloc(sizeof(struct linkedlist));
printf("Enter value for the node:\n");
scanf("%d",&node->data);
if(first == NULL) {
node->prev = NULL;
node->next = NULL;
first = node;
last = node;
printf("Linked list Created!\n");
}
else
{
node->prev = NULL;
first->prev = node;
node->next = first;
first = node;
printf("Data Inserted at the beginning of the Linked list!\n");
}
}
void insert_end()
{
node = (struct linkedlist*)malloc(sizeof(struct linkedlist));
printf("Enter value for the node:\n");
scanf("%d",&node->data);
if(first == NULL) {
node->prev = NULL;
node->next = NULL;
first = node;
last = node;
printf("Linked list Created!\n");
}
else
{
node->next = NULL;
last->next = node;
node->prev = last;
last = node;
printf("Data Inserted at the end of the Linked list!\n");
}
}
void display()
{
node = first;
printf("List of data in Linked list in Ascending order!\n");
while(node != NULL) {
printf("%d\n",node->data);
node = node->next;
}
node = last;
printf("List of data in Linked list in Descending order!\n");
while(node != NULL) {
printf("%d\n",node->data);
node = node->prev;
}
}
void delete_()
{
int count = 0, number, i;
node = node1 = node2 = first;
for(node = first; node != NULL; node = node->next)
printf("Enter value :\n");
count++;
display();
printf("\n%d no of nodes\n", count);
printf("Enter the node number which you want to delete:\n");
scanf("%d", &number);
if(number != 1) {
if(number < count && number > 0) {
for(i = 2; i <= number; i++)
node = node->next;
for(i = 2; i <= number-1; i++)
node1 = node1->next;
for(i = 2; i <= number+1; i++)
node2 = node2->next;
node2->prev = node1;
node1->next = node2;
node->prev = NULL;
node->next = NULL;
free(node);
}
else if(number == count) {
node = last;
last = node->prev;
last->next = NULL;
free(node);
}
else
printf("Invalid number!\n");
}
else {
node = first;
first = node->next;
first->prev = NULL;
free(node);
}
printf("Node has been deleted successfully!\n");
int main() {
int op = 0;
while(op != 5) {
printf("1. Insert at the beginning\n2. Insert at the end\n3. Delete\n4. Display\n5.
Exit\n");
printf("Enter your choice:\n");
scanf("%d", &op);
switch(op) {
case 1:
insert_beginning();
break;
case 2:
insert_end();
break;
case 3:
delete_();
break;
case 4:
display();
break;
case 5:
exit(0);
break;
default:
printf("Invalid choice!\n");
}
}
return 0;
}
OUTPUT
1. Insert at the beginning
2. Insert at the end
3. Delete
4. Display
5. Exit
Enter your choice:
1
Enter value for the node:
2
Linked list Created!
1. Insert at the beginning
2. Insert at the end
3. Delete
4. Display
5. Exit
Enter your choice:
1
Enter value for the node:
3
Data Inserted at the beginning of the Linked list!
1. Insert at the beginning
2. Insert at the end
3. Delete
4. Display
5. Exit
Enter your choice:
4
List of data in Linked list in Ascending order!
3
2
List of data in Linked list in Descending order!
2
3
1. Insert at the beginning
2. Insert at the end
3. Delete
4. Display
5. Exit
Enter your choice:
3
Enter value :
Enter value :
List of data in Linked list in Ascending order!
3
2
List of data in Linked list in Descending order!
2
3
1 no of nodes
Enter the node number which you want to delete:
3
Node has been deleted successfully!
1. Insert at the beginning
2. Insert at the end
3. Delete
4. Display
5. Exit
Enter your choice:
4
List of data in Linked list in Ascending order!
2
List of data in Linked list in Descending order!
2
1. Insert at the beginning
2. Insert at the end
3. Delete
4. Display
5. Exit
Enter your choice:
1
Enter value for the node:
9
Data Inserted at the beginning of the Linked list!
1. Insert at the beginning
2. Insert at the end
3. Delete
4. Display
5. Exit
Enter your choice:
2
Enter value for the node:
0
Data Inserted at the end of the Linked list!
1. Insert at the beginning
2. Insert at the end
3. Delete
4. Display
5. Exit
Enter your choice:
4
List of data in Linked list in Ascending order!
9
2
0
List of data in Linked list in Descending order!
0
2
9
1. Insert at the beginning
2. Insert at the end
3. Delete
4. Display
5. Exit
Enter your choice:
5
--------------------------------
Process exited after 50.03 seconds with return value 0
Press any key to continue . . .