DS Lab
DS Lab
DS Lab
#include <stdio.h>
int main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
return 0;
}
Multiplication
#include <stdio.h>
#include <stdlib.h>
int main()
{
int r1, r2, c1, c2;
printf("\n");
}
return 0;
}
1. Write a program to perform various string operations such as copy, length, reversing,
palindrome, concatenation and to find occurrence of a sub-string using and without using
library functions.
#include
#include
#include
void main()
{
int no,m,ch ,i,j,n,k,flag=0;
char str[20],str2[20],str3[20],str4[20];
char wish;
clrscr();
do
{
menu:
printf(“\n1.length of the string.”);
printf(“\n2.copying string another.”);
printf(“\n3.concatination of two string”);
printf(“\n4.reversing the string”);
printf(“\n5.checking palindrome or not.”);
printf(“\n6.checking sub-string in the string.” );
printf(“\nEnter the choice:”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
printf(“Enter the string: “);
flushall();
gets(str);
for(i=1;str[i]!=’\0′;i++); //to find the length of the string
printf(“the length of the string is:%d “,i);
break;
case 2:
printf(“Enter the string: “);
flushall();
gets(str);
for(i=0;str[i]!=’\0′;i++)//TO COPY A STRING IN OTHER
{
str2[i]=str[i];
}
str2[i]=’\0′;
printf(“\n the copied string is: “);
puts(str2);
break;
case 3:
printf(“Enter the string: “);
flushall();
gets(str);
printf(“\n Enter the second string”);
flushall();
gets(str2);
i=0;
while(str[i]!=’\0′)
i++;
for(j=0;str2[j]!=’\0′;j++,i++)//TO CONCATINATE TWO STRING
str[i]=str2[j];
str[i]=’\0′;
printf(“\nthe concatinated string is: “);
puts(str);
break;
case 4:
printf(“Enter the string: “);
flushall();
gets(str);
i=0;
while(str[i]!=’\0′)
i++;
i–;
for(j=0;str[j]!=’\0′,i>=0;j++,i–)//TO REVERSE STRING
str2[j]=str[i];
str2[j]=’\0′;
printf(“\n the reverse string is: “);
puts(str2);
break;
case 5:
printf(“Enter the string: “);
flushall();
gets(str);
i=0;
while(str[i]!=’\0′)
i++;
i–;
flag=1;
for(j=0;i>j;j++,i–)
{
if(str[i]!=str[j])
{
flag=0;
break;
}
}
if(flag==1)
printf(“\nthe string is palindrom.”);
else
printf(“\nthe string is not palindrom.”);
break;
case 6:
printf(“Enter the string: “);
flushall();
gets(str);
printf(“\nEnter the second string: “); //TO FIND WETHER SUBSTRING EXISTS OR NOT
gets(str4);
for(m=0;str[m]!=’\0′;m++);
for(k=0;k<=m-1;k++)
{
for(j=0;str4[j]!=’\0′;j++)
{
if(str[k+j]!=str4[j])
{
flag=0;
break;
}
}
if(str4[j]==’\0′)
{
flag=1;break;}
}
if(flag==1)
printf(“\nsubstring found at position:%d “,j+k);
else
printf(“\n substring donot exists”);
break;
default:
printf(“\n wrong choice!!!!”);
break;
}
printf(“\nwant to continue(y/n)”);
scanf(“%c”,&wish);
}
while(wish!=’n’);
getch();
}
2.Write a program to search an element using Linear Search.
Answer:
#include <stdio.h>
int main()
{
int array[100], search, c, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d integer(s)\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter a number to search\n");
scanf("%d", &search);
for (c = 0; c < n; c++)
{
if (array[c] == search) /* If required element is found */
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if (c == n)
printf("%d isn't present in the array.\n", search);
return 0;
}
Binary Search Algorithm can be implemented in two ways which are discussed below.
1. Iterative Method
2. Recursive Method
The recursive method follows the divide and conquer approach.
The general steps for both methods are discussed below.
Initial array
Let x = 4 be the element to be searched.
2. Set two pointers low and high at the lowest and the highest positions respectively.
Setting pointers
3. Find the middle element mid of the array ie. arr[(low + high)/2] = 6.
Mid element
4. If x == mid, then return mid.Else, compare the element to be searched with m.
5. If x > mid, compare x with the middle element of the elements on the right side of mid. This
is done by setting low to low = mid + 1.
6. Else, compare x with the middle element of the elements on the left side of mid. This is done
by setting high to high = mid - 1.
Mid element
7. x = 4 is found.
Found
Binary Search Algorithm
Iteration Method
if (x == arr[mid])
return mid
low = mid + 1
high = mid - 1
// Binary Search in C
#include <stdio.h>
if (array[mid] == x)
return mid;
if (array[mid] < x)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
int main(void) {
int array[] = {3, 4, 5, 6, 7, 8, 9};
int n = sizeof(array) / sizeof(array[0]);
int x = 4;
int result = binarySearch(array, x, 0, n - 1);
if (result == -1)
printf("Not found");
else
printf("Element is found at index %d", result);
return 0;
}
Recursive Method
return False
else
if x == arr[mid]
return mid
// Binary Search in C
#include <stdio.h>
return -1;
}
int main(void) {
int array[] = {3, 4, 5, 6, 7, 8, 9};
int n = sizeof(array) / sizeof(array[0]);
int x = 4;
int result = binarySearch(array, x, 0, n - 1);
if (result == -1)
printf("Not found");
else
printf("Element is found at index %d", result);
}
merge(arr, l, m, r);
}
}
/* UTILITY FUNCTIONS */
/* Function to print an array */
void printArray(int A[], int size)
{
int i;
for (i = 0; i < size; i++)
printf("%d ", A[i]);
printf("\n");
}
/* Driver code */
int main()
{
int arr[] = { 12, 11, 13, 5, 6, 7 };
int arr_size = sizeof(arr) / sizeof(arr[0]);