0% found this document useful (0 votes)
71 views10 pages

PPL Practical File

na

Uploaded by

nijeme9208
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views10 pages

PPL Practical File

na

Uploaded by

nijeme9208
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PRACTICAL FILE

SESSION: 2023-24
Principles of Programming
Languages Lab
(ETCS 458)
IV Year, VIII Sem

Submitted to: Submitted by:


Name: Name:

Designation: Enrollment No.

Department of Computer Science and Engineering


Delhi Technical Campus, Greater Noida
INDEX

[Link]. PROGRAM NAME DATE OF DATE OF SIGN.


EXPERIMENT SUBMISSION
1 Implement all major functions of string.h
in single C program using switch case to
select specific function from user choice
(like strlen, strcat, strcpy, strcmp, strrev).
2 Write a program (WAP) in C to reverse a
linked list iterative and recursive.
3 WAP in C to implement iterative Towers
of Hanoi.
4 WAP in C++ to count the no. of
object of a class with the help of
static data member, function
and constructor.
5

8
9
10

11
Experiment 1

Aim: Implement all major functions of string.h in single C program using switch case to select
specific function from user choice (like strlen, strcat, strcpy, strcmp, strrev).

Code:
#include <stdio.h>
#include <string.h>

void get_string(char *str);


void custom_strcpy(char *dest, const char *src);
void custom_strcat(char *dest, const char *src);
int custom_strcmp(const char *str1, const char *str2);
void custom_strrev(char *str);

int main() {
char str1[100], str2[100], result[100];
int choice;
printf("Enter your choice:\n");
printf("1. strlen\n");
printf("2. strcat\n");
printf("3. strcpy\n");
printf("4. strcmp\n");
printf("5. strrev\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter a string: ");
get_string(str1);
printf("Length of the string: %d\n", strlen(str1));
break;
case 2:
printf("Enter the first string: ");
get_string(str1);
printf("Enter the second string: ");
get_string(str2);
custom_strcat(str1, str2);
printf("Concatenated string: %s\n", str1);
break;
case 3:
printf("Enter the source string: ");
get_string(str1);
custom_strcpy(result, str1);
printf("Copied string: %s\n", result);
break;
case 4:
printf("Enter the first string: ");
get_string(str1);
printf("Enter the second string: ");
get_string(str2);
printf("Result of strcmp: %d\n", custom_strcmp(str1, str2));
break;
case 5:
printf("Enter a string: ");
get_string(str1);
custom_strrev(str1);
printf("Reversed string: %s\n", str1);
break;
default:
printf("Invalid choice\n");
}
return 0;
}
void get_string(char *str) {
scanf(" %[^\n]s", str);
}
void custom_strcpy(char *dest, const char *src) {
while (*src) {
*dest = *src;
src++;
dest++;
}
*dest = '\0';
}
void custom_strcat(char *dest, const char *src) {
while (*dest) {
dest++;
}
while (*src) {
*dest = *src;
src++;
dest++;
}
*dest = '\0';
}
int custom_strcmp(const char *str1, const char *str2) {
while (*str1 && *str2 && *str1 == *str2) {
str1++;
str2++;
}
return *str1 - *str2;
}
void custom_strrev(char *str) {
int length = strlen(str);
char *start = str;
char *end = str + length - 1;

while (start < end) {


char temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
}

Output:
Experiment 2

Aim: Write a program (WAP) in C to reverse a linked list iterative and recursive.

Code:

#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node* next;
};
struct Node* createNode(int data);
struct Node* reverseIterative(struct Node* head);
struct Node* reverseRecursive(struct Node* head);
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed\n");
exit(1);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}
struct Node* reverseIterative(struct Node* head) {
struct Node *prev = NULL, *curr = head, *next;
while (curr != NULL) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
head = prev;
return head;
}
struct Node* reverseRecursive(struct Node* head) {
if (head == NULL || head->next == NULL)
return head;
struct Node* rest = reverseRecursive(head->next);
head->next->next = head;
head->next = NULL;
return rest;
}
void printList(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
struct Node* head = createNode(1);
head->next = createNode(2);
head->next->next = createNode(3);
head->next->next->next = createNode(4);
head->next->next->next->next = createNode(5);
printf("Original Linked List: ");
printList(head);
head = reverseIterative(head);
printf("Reversed Linked List (Iterative): ");
printList(head);
head = reverseRecursive(head);
printf("Reversed Linked List (Recursive): ");
printList(head);
return 0;
}

Output:
Experiment 3

Aim: WAP in C to implement iterative Towers of Hanoi.

Code:

#include <stdio.h>
#include <stdlib.h>
void iterativeTowersOfHanoi(int num_disks, char source, char auxiliary, char destination) {
int total_moves = (1 << num_disks) - 1; // Equivalent to 2^num_disks - 1
int move = 0;
while (move < total_moves) {
int from_peg = (move & move + 1) % 3;
int to_peg = ((move | move + 1) + 1) % 3;
printf("Move disk from peg %c to peg %c\n", (from_peg == 0 ? source : (from_peg == 1
? auxiliary : destination)), (to_peg == 0 ? source : (to_peg == 1 ? auxiliary : destination)));
move++;
}
}
int main() {
int num_disks;
printf("Enter the number of disks: ");
scanf("%d", &num_disks);
iterativeTowersOfHanoi(num_disks, 'A', 'B', 'C');
return 0;
}

Output:
Experiment 4

Aim: WAP in C++ to count the no. of object of a class with the help of static data member,
function and constructor.

Code:

#include <iostream>

class MyClass
{
private:
static int count;
public:
MyClass()
{
count++;
std::cout << "Object created. Total objects: " << count << std::endl;
}

~MyClass()
{
count--;
std::cout << "Object destroyed. Total objects: " << count << std::endl;
}

static int getCount()


{
return count;
}
};

int MyClass::count = 0;

int main()
{
MyClass obj1;
MyClass obj2;
MyClass obj3;
std::cout << "Number of objects created: " << MyClass::getCount() << std::endl;

MyClass *obj4 = new MyClass();


std::cout << "Number of objects created: " << MyClass::getCount() << std::endl;

delete obj4;

std::cout << "Number of objects created: " << MyClass::getCount() << std::endl;

return 0;
}

Output:

You might also like