0% found this document useful (0 votes)
31 views16 pages

Ds File Original

The document contains code for several C programs that perform operations on arrays: 1) A program to check if a string is a palindrome by reversing the string and comparing it to the original. 2) A linear search program to search an array for a given item. 3) Programs for inserting, deleting, and sorting elements in a linear array. 4) A binary search program to search a sorted array for a given item. The programs are accompanied by sample inputs, outputs and descriptions of the programs.

Uploaded by

Veekey Aqida
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views16 pages

Ds File Original

The document contains code for several C programs that perform operations on arrays: 1) A program to check if a string is a palindrome by reversing the string and comparing it to the original. 2) A linear search program to search an array for a given item. 3) Programs for inserting, deleting, and sorting elements in a linear array. 4) A binary search program to search a sorted array for a given item. The programs are accompanied by sample inputs, outputs and descriptions of the programs.

Uploaded by

Veekey Aqida
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Data Structures

Program file

Submitted by : Jaskarandeep Punia


Roll No. : 10901140
Class : B.Tech 2nd year , 3rd Semester
Group : CE-4
 Programe to detemine whether given string is pallindrome
or not.
#include<conio.h>

#include<string.h>

void main()

char s[999],t[999];

clrscr();

printf("enter the string\t");

scanf("%s",&s);

strcpy(t,s);

strrev(t);

if(strcmp(s,t)==0)

printf("pallindrome");

else

printf("non pallindrome");

getch();

}
OUTPUT :
 Case 1.

enter the string madam

pallindrome

 Case 2.

enter the string jaskaran

non pallindrome
 Programe for Linear Search in array.

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

int n,i,item,a[100];

clrscr();

printf("enter the no. of elements of array");

scanf("%d",&n);

printf("\n enter the elements of array");

for(i=0;i<=n;i++)

scanf("%d",&a[i]);

printf("\n enter the item");

scanf("%d",&item);

for(i=0;i<=n;i++)

{
if(a[i]==item)

printf("\n item is found at location = %d",i+1);

break;

if(a[i]!=item)

printf("\n item not found so lacation = NULL");

getch();

OUTPUT :
enter the no. of elements of array 3

enter the elements of array 1

enter the item 3

item is found at location =3


 Program for inserting element in a linear array.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int n,k,a[100]={1,2,3,4},i,item;
clrscr();
printf("\n enter the no of elements of array");
scanf("%d",&n);
printf("enter the location k");
scanf("%d",&k);
for(i=n;i>=k;i--)
{
a[i+1]=a[i];
}
printf("\n enter the item");
scanf("%d",&item);
a[k]=item;
n=n+1;
printf("array is");
for(i=0;i<=n;i++)
{
printf("\n a[%d]=%d",i,a[i]);
}
getch();
}
OUTPUT :

enter the no of elements of array 5


enter the location k 2
enter the item 9
array is
a[0]=1
a[0]=2
a[0]=9
a[0]=3
a[0]=4
a[0]=0
a[0]=0
 Program for deleting element from linear array.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int n,k,a[100]={1,2,3,4},i,item;
clrscr();
printf("\n enter the no of elements of array");
scanf("%d",&n);
printf("enter the location k");
scanf("%d",&k);
item=a[k];
for(i=k;i<=n;i++)
{
a[i]=a[i+1];
}
n=n-1;
printf("array is");
for(i=0;i<=n;i++)
{
printf("\n a[%d]=%d",i,a[i]);
}
getch();
}
OUTPUT :
enter the no of array 6
enter the location 2
array is
a[0]=1
a[0]=2
a[0]=4
a[0]=0
a[0]=0
a[0]=0
 Program for sorting elements of an array (Bubble Sort)
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int a[100],n,i,ptr,d;
clrscr();
printf("enter the no. of elements");
scanf("%d",&n);
printf("\n enter the array");
for(i=0;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<=n-1;i++)
{
ptr=0;
while(ptr<=n-i)
{
if(a[ptr]>a[ptr+1])
{
d=a[ptr+1];
a[ptr+1]=a[ptr];
a[ptr]=d;
}
ptr=ptr+1;
}
}
printf("\n array is");
for(i=0;i<=n;i++)
{
printf("\n a[%d]=%d",i,a[i]);
}
getch();
}

OUTPUT :
enter the no. of elements 5
enter the array 84
74
98
2
6
74
array is
a[0]=2
a[0]=6
a[0]=74
a[0]=74
a[0]=84
a[0]=98
 Program for Binary Search in array.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int beg,n,end,mid,i,item,a[100];
clrscr();
printf("enter the no. of elements of array");
scanf("%d",&n);
beg=0;
end=n;
mid=(beg+end)/2;
printf("\n enter the elements of array");
for(i=0;i<=n;i++)
{
scanf("%d",&a[i]);
}
printf("\n enter the item");
scanf("%d",&item);
while(beg<=end&&a[mid]!=item)
{
if(item<a[mid])
{
end=mid-1;
}
else
{
beg=mid+1;
}
mid=(beg+end)/2;
}
if(a[mid]==item)
{
printf("\n item is found at location = %d",mid+1);
}
else
{
printf("\n item not found so location = NULL");
}
getch();
}

OUTPUT :

enter the no. of elements of array 4


enter the elements of array 1
2
3
4
5
enter the item 4
item is found at location = 4

You might also like