Practicals 1
Practicals 1
int main() {
int choice;
char str1[100], str2[100], result[200];
do {
printf("\nMenu:\n");
printf("1. String Length\n");cl
printf("2. String Concatenation\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
getchar(); // To consume newline left by scanf
switch (choice) {
case 1:
printf("Enter a string: ");
gets(str1);
printf("Length of the string: %d\n", string_length(str1));
break;
case 2:
printf("Enter first string: ");
gets(str1);
printf("Enter second string: ");
gets(str2);
string_concatenation(str1, str2, result);
printf("Concatenated String: %s\n", result);
break;
case 3:
printf("Exiting the program.\n");
break;
default:
printf("Invalid choice! Please enter a valid option.\n");
}
} while (choice != 3);
return 0;
}
Output:
Menu:
1. String Length
2. String Concatenation
3. Exit
Enter your choice: 1
Enter a string: Hello World
Length of the string: 11
Menu:
1. String Length
2. String Concatenation
3. Exit
Enter your choice: 2
Enter first string: Hello
Enter second string: World
Concatenated String: HelloWorld
Menu:
1. String Length
2. String Concatenation
3. Exit
Enter your choice: 3
Exiting the program.
2.Write a C program to find a pattern in a given text using pattern matching
algorithm.
.#include<stdio.h>
#include<string.h>
void patternMatching(char text[], char pattern[])
{
int textLen = strlen(text); // Length of text
int patternLen = strlen(pattern); // Length of pattern
int found = 0;
// Loop through text to check each character
for(int i = 0; i <= textLen - patternLen; i++) {
int j;
// Check if pattern matches at index i
for(j = 0; j < patternLen; j++) {
if(text[i + j] != pattern[j]) {
break; // If characters don't match, break loop
}
}
if(j == patternLen) { // If full pattern matches
printf("Pattern found at index %d\n", i);
found = 1;
}
}
if(found == 0) {
printf("Pattern not found.\n");
}
}
int main() {
char text[100], pattern[50];
printf("Enter the text: ");
gets(text); // Input the text
printf("Enter the pattern to search: ");
gets(pattern); // Input the pattern
patternMatching(text, pattern); // Function call
return 0;
}
Input:
Enter the text: welcome to coding
Enter the pattern to search: coding
Output:
Pattern found at index 11
3. Write a program to search for an element in an array using linear search.
#include <stdio.h>
int main() {
int n, i, key, found = 0;
// Input number of elements
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n]; // Array declaration
// Input array elements
printf("Enter %d elements: ", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Input the element to search
printf("Enter element to search: ");
scanf("%d", &key);
// Linear Search algorithm
for (i = 0; i < n; i++) {
if (arr[i] == key) {
printf("Element found at index %d\n", i);
found = 1;
break; // Exit loop when element is found
}
}
Output:
Enter number of elements: 5
Enter 5 elements: 5 3 8 1 2
Sorted array: 1 2 3 5 8
5.Perform the Insertion sort on the input {75,8,1,16,48,3,7,0} and display the
output in descending order.
#include <stdio.h>
int main()
{
int arr[] = {75, 8, 1, 16, 48, 3, 7, 0};
// Insertion Sort in descending order
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] < key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
// Step 3: Move the n-1 disks from auxiliary to destination using source as
auxiliary
towerOfHanoi(n - 1, auxiliary, source, destination);
}
int main() {
int n;
// Input the number of disks
printf("Enter the number of disks: ");
scanf("%d", &n);
// Call the function to solve the problem
printf("\nSteps to solve the Towers of Hanoi for %d disks:\n", n);
towerOfHanoi(n, 'A', 'B', 'C'); // A is the source, B is the auxiliary, C is the
destination
return 0;
}
Output:
Enter the number of disks: 3
Steps to solve the Towers of Hanoi for 3 disks:
Move disk 1 from A to C
Move disk 2 from A to B
Move disk 1 from C to B
Move disk 3 from A to C
Move disk 1 from B to A
Move disk 2 from B to C
Move disk 1 from A to C
7 .Write a Program to implement stack using array
#include <stdio.h>
#define MAX 5 // Maximum size of the stack
int stack[MAX];
int top = -1; // Stack is initially empty
// Function to check if the stack is full
int isFull() {
return top == MAX - 1;
}
8.Write a program to insert the elements {45, 34, 10, 63,3} into linear queue
and delete three elements from the list. Display your list after each insertion
and deletion.
#include <stdio.h>
#define SIZE 10
int queue[SIZE];
int front = -1, rear = -1;
Output:
Inserted 45 -> Queue: 45
Inserted 34 -> Queue: 45 34
Inserted 10 -> Queue: 45 34 10
Inserted 63 -> Queue: 45 34 10 63
Inserted 3 -> Queue: 45 34 10 63 3
Deleted 45 -> Queue: 34 10 63 3
Deleted 34 -> Queue: 10 63 3
Deleted 10 -> Queue: 63 3
PART B
11.Given S1={“Flowers”} ; S2={“are beautiful”} I. Find the length of S1 II.
Concatenate S1 and S2 III. Extract the substring “low” from S1 IV. Find “are” in
S2 and replace it with “is” .
#include <stdio.h>
#include <string.h>
int main()
{
// Define the strings
char S1[] = "Flowers";
char S2[] = "are beautiful";
char substring[] = "low"; // Substring we want to extract from S1
char temp[100]; // To store the result of replacement
n2.data = 20;
n2.next = &n3;
n3.data = 30;
n3.next = NULL; // Last node points to NULL
if (isalnum(ch)) {
postfix[j++] = ch;
}
else if (ch == '(') {
stack[++top] = ch;
}
else if (ch == ')') {
while (top != -1 && stack[top] != '(') {
postfix[j++] = stack[top--];
}
top--; // pop '('
}
else { // operator
// precedence manually checked for simplicity
while (top != -1 && stack[top] != '(' &&
((ch != '^' && (stack[top] == '^' || stack[top] == '*' || stack[top] == '/'
|| stack[top] == '+' || stack[top] == '-')) || (ch == '+' || ch == '-') && (stack[top]
== '*' || stack[top] == '/' || stack[top] == '^') || (ch == '*' || ch == '/') &&
(stack[top] == '^')))
{
postfix[j++] = stack[top--];
}
stack[++top] = ch;
}
i++;
}
while (top != -1) {
postfix[j++] = stack[top--];
}
postfix[j] = '\0';
printf("Postfix Expression: %s\n", postfix);
return 0;
}
Output:
Postfix Expression: xy^5z*/2+
if (isdigit(ch)) {
int num = ch - '0';
stack[++top] = num;
}
else if (ch == '+' || ch == '-' || ch == '*' || ch == '/') {
int val2 = stack[top--];
int val1 = stack[top--];
int result;
if (ch == '+')
result = val1 + val2;
else if (ch == '-')
result = val1 - val2;
else if (ch == '*')
result = val1 * val2;
else
result = val1 / val2;
stack[++top] = result;
}
i++;
}
printf("Result of the postfix expression: %d\n", stack[top]);
return 0;
}
Output:
Result of the postfix expression: 48
15 Write a C program to simulate the working of Linear Queue using an array.
#include <stdio.h>
#define SIZE 5 // Size of the queue
int queue[SIZE];
int front = -1, rear = -1;
int main() {
// Inserting elements into the queue
enqueue(10);
enqueue(20);
enqueue(30);
// Displaying the queue
display();
// Deleting an element from the queue
dequeue();
dequeue();
// Displaying the queue again
display();
return 0;
}
Output:
Inserted 10
Inserted 20
Inserted 30
Queue: 10 20 30
Deleted 10
Deleted 20
Queue: 30