1) MENU DRIVEN PROGRAM TO FIND THE LENGTH OF A STRING AND STRING
CONCATENATION
AIM:
To write a menu driven program to find the length of a string and concatenate
the two string without the use of String functions.
ALGORITHM:
1. Start
2. Input two strings as first string and second string.
3. Initialize length of the string as zero.
4. Check whether length of the first string is zero or not.
5. If not, increment the length and print the length of the first string.
6. Assign the first string value to the second string value.
7. Add the null character at the end for the result.
8.Print the concatenated string.
9.Stop the process.
PROGRAM:
#include <stdio.h>
int stringLength(char str[])
{
int length = 0;
while (str[length] != '\0')
{
length++;
}
return length;
}
void stringConcatenate(char str1[], char str2[])
{
int i = 0, j = 0;
while (str1[i] != '\0')
{
i++;
}
while (str2[j] != '\0')
{
str1[i] = str2[j];
i++;
j++;
}
str1[i] = '\0';
}
int main()
{
char str1[100], str2[100];
int choice;
printf("Enter the first string: ");
scanf("%s", str1);
printf("Enter the second string: ");
scanf("%s", str2);
do
{
printf("Choose an operation:\n");
printf("1. String Length\n");
printf("2. String Concatenation\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Length of first string: %d\n", stringLength(str1));
printf("Length of second string: %d\n", stringLength(str2));
break;
case 2:
stringConcatenate(str1, str2);
printf("Concatenated string: %s\n", str1);
break;
case 3:
printf("Exiting the program. Goodbye!\n");
return 0;
default:
printf("Invalid choice! Please enter a valid option.\n");
break;
}
}while(choice!=3);
return 0;
}
2) Searching an Element Using Binary Search
AIM:
To search an element in a sorted array and find it whether it exists or not using
Binary Search.
Algorithm:
1. All the elements must be in sorted order.
2. Find the MID element, compare the MID element with given Key.
3. If Key matched with MID, return.
4. If Key is less than the MID, go ahead with Left sub array and start from
step 2.
5. If Key is Greater than the MID, go ahead with right sub array and start
from step 2.
6. Repeat this process until you found a key or return with no key found.
Program:
int main()
{
int c, first, last, middle, n, search, array[100];
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d",&array[c]);
printf("Enter value to find\n");
scanf("%d",&search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while( first <= last )
{
if ( array[middle] < search )
first = middle + 1;
else if ( array[middle] == search )
{
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if ( first > last )
printf("Not found! %d is not present in the list.\n", search);
return 0;
}
3. Sorting of N Elements Using Selection Sort
AIM:
To Write a C program to sort a list of N elements using Selection Sort Algorithm.
ALGORITHM:
1. Declare an array of size, n.
2. Provide the n inputs such that the array is unsorted.
3. Loop i, from 0 to n-2
4. Inner loop j runs from i+1 to n-1
5. On each iteration of j, check:
If arr[i] > arr[i] then we swap the number in order to sort the
array in ascending order.
6. Print the sorted array
Program:
#include<stdio.h>
int main(){
int a[10],i;
int j,temp,num;
printf("Enter the number to give\n");
scanf("%d",&num);
for(i=0; i<num; i++){
printf("a[%d]=\t",i);
scanf("%d",&a[i]);
}
for(i=0; i<num-1; i++){
for(j=i+1;j<num; j++){
if(a[i]>a[j]){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("Selection Sort in C\n");
for(i=0; i<num; i++){
printf("a[%d]=\t%d\n",i,a[i]);
}
}
4. Demonstration of Queue Using Linked List
AIM:
To write a C program for demonstrating Queue using Linked List.
ALGORITHM:
1. Allocate the space for the new node PTR
2. SET PTR -> DATA = VAL
3. IF FRONT = NULL
SET FRONT = REAR = PTR
SET FRONT -> NEXT = REAR -> NEXT = NULL
ELSE
SET REAR -> NEXT = PTR
SET REAR = PTR
SET REAR -> NEXT = NULL
END OF IF
4. IF FRONT = NULL
Write " Underflow "
Go to Step 5
END OF IF
5. SET PTR = FRONT
6. SET FRONT = FRONT -> NEXT
7. FREE PTR
8. END
Program:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front;
struct node *rear;
void insert();
void delete();
void display();
void main ()
{
int choice;
while(choice != 4)
{
printf("\n*************************Main Menu*****************************\
n");
printf("\
n================================================
==========\n");
printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\
n4.Exit\n");
printf("\nEnter your choice ?");
scanf("%d",& choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nEnter valid choice??\n");
}
}
}
void insert()
{
struct node *ptr;
int item;
ptr = (struct node *) malloc (sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
return;
}
else
{
printf("\nEnter value?\n");
scanf("%d",&item);
ptr -> data = item;
if(front == NULL)
{
front = ptr;
rear = ptr;
front -> next = NULL;
rear -> next = NULL;
}
else
{
rear -> next = ptr;
rear = ptr;
rear->next = NULL;
}
} }
void delete ()
{
struct node *ptr;
if(front == NULL)
{
printf("\nUNDERFLOW\n");
return; }
else
{
ptr = front;
front = front -> next;
free(ptr);
}
}
void display()
{
struct node *ptr;
ptr = front;
if(front == NULL)
{
printf("\nEmpty queue\n");
}
else
{ printf("\nprinting values .....\n");
while(ptr != NULL)
{
printf("\n%d\n",ptr -> data);
ptr = ptr -> next;
}
}
}