0% found this document useful (0 votes)
16 views3 pages

Dsa Lab 1-5

The document contains multiple C programming lab programs that cover various topics such as calendar creation, string pattern matching, stack operations, infix to postfix conversion, and postfix evaluation. Each program includes function definitions, memory management, and user input handling. The programs demonstrate fundamental concepts in data structures and algorithms.

Uploaded by

sheshankhadpad90
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)
16 views3 pages

Dsa Lab 1-5

The document contains multiple C programming lab programs that cover various topics such as calendar creation, string pattern matching, stack operations, infix to postfix conversion, and postfix evaluation. Each program includes function definitions, memory management, and user input handling. The programs demonstrate fundamental concepts in data structures and algorithms.

Uploaded by

sheshankhadpad90
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
You are on page 1/ 3

LAB PROGRAM 1: CALENDER

#include <stdio.h>
LAB PROGRAM 2: STRING PATTERN MATCHING
#include <stdlib.h> OUTPUT: #include <stdio.h>
#include <string.h>
typedef struct { #include <string.h>
char *dayName;
int date; void replacePattern(char *str, char *pat, char *rep) {
char *activity;
} Day; char res[100] = "";
Day* createCalendar() {
Day *calendar = malloc(7 * sizeof(Day));
char *pos = str, *match;
if (!calendar) {
printf("Memory allocation failed!\n");
while ((match = strstr(pos, pat))) {
exit(1); strncat(res, pos, match - pos); OUTPUT:
}
for (int i = 0; i < 7; i++) { strcat(res, rep);
calendar[i].dayName = malloc(20 * sizeof(char));
calendar[i].activity = malloc(100 * sizeof(char)); pos = match + strlen(pat);
if (!calendar[i].dayName || !calendar[i].activity) {
printf("Memory allocation failed!\n"); }
}
exit(1);
strcat(res, pos);
}
return calendar;
printf("Result: %s\n", res);
}
void readCalendar(Day* cal) {
}
for (int i = 0; i < 7; i++) { int main() {
printf("Enter Day Name, Date, Activity: ");
scanf("%s %d ", cal[i].dayName, &cal[i].date); char str[100], pat[20], rep[20];
fgets(cal[i].activity, 100, stdin);
size_t len = strlen(cal[i].activity); printf("Enter the main string: ");
if (len > 0 && cal[i].activity[len - 1] == '\n') {
cal[i].activity[len - 1] = '\0'; fgets(str, sizeof(str), stdin);
}
}
str[strcspn(str, "\n")] = '\0';
}
void displayCalendar(Day* cal) {
printf("Enter the pattern to replace: ");
for (int i = 0; i < 7; i++) { scanf("%s", pat);
printf("%s %d %s\n", cal[i].dayName, cal[i].date, cal[i].activity);
} printf("Enter the replacement string: ");
}
int main() { scanf("%s", rep);
Day *calendar = createCalendar();
readCalendar(calendar); replacePattern(str, pat, rep);
printf("\nCalendar:\n");
displayCalendar(calendar);
return 0;
for (int i = 0; i < 7; i++) {
free(calendar[i].dayName);
}
free(calendar[i].activity);
}
free(calendar);
return 0;
}
LAB PROGRAM 3: STACK OPERATION LAB PROGRAM 4: INFIX TO POSTFIX

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


#define MAX 50 #include <ctype.h>
int stack[MAX], top = -1; #define MAX 50
void push(int val) int precedence(char op) { return (op == '+' || op == '-') ? 1 : (op == '*' || op == '/') ? 2 : 0; }
{ top < MAX - 1 ?stack[++top] = val : printf("Stack overflow!\n"); } void infixToPostfix(char *infix, char *postfix) {
int pop() char stack[MAX];
{ return top >= 0 ? stack[top--] : (printf("Stack underflow!\n"), -1); } int top = -1, j = 0;
void display() { for (int i = 0; infix[i]; i++) {
if (top == -1) printf("Stack is empty!\n"); if (isalnum(infix[i])) postfix[j++] = infix[i];
else for (int i = top; i >= 0; i--) printf("%d ", stack[i]); else if (infix[i] == '(') stack[++top] = infix[i];
printf("\n"); else if (infix[i] == ')')
} while (stack[top] != '(') postfix[j++] = stack[top--];
int main() { else {
int choice, value; while (top >= 0 && precedence(stack[top]) >= precedence(infix[i])) postfix[j++] = stack[top--];
while (1) { stack[++top] = infix[i];
printf("\n1. Push 2. Pop 3. Display 4. Exit\nChoice: "); }
scanf("%d", &choice); }
if (choice == 4) break; while (top >= 0) postfix[j++] = stack[top--];
switch (choice) { postfix[j] = '\0'; OUTPUT:
case 1: printf("Value: "); scanf("%d", &value); push(value); break; }
case 2: if ((value = pop()) != -1) printf("Popped: %d\n", value); break; int main() {
case 3: display(); break; char infix[MAX], postfix[MAX];
default: printf("Invalid choice!\n"); printf("Enter an infix expression: ");
} scanf("%s", infix);
} OUTPUT: infixToPostfix(infix, postfix);
return 0; printf("Postfix expression: %s\n", postfix);
} return 0;
}
LAB PROGRAM 5:EVALUATE POSTFIX

#include <stdio.h>
#include <ctype.h> OUTPUT:
#define MAX 50
int stack[MAX], top = -1;
void push(int val) { stack[++top] = val; }
int pop() { return stack[top--]; }
int evalPostfix(char *postfix) {
for (int i = 0; postfix[i]; i++) {
if (isdigit(postfix[i])) push(postfix[i] - '0');
else {
int b = pop(), a = pop();
if (postfix[i] == '/' && b == 0) return printf("Division by zero!\n"), -1;
push(postfix[i] == '+' ? a + b : postfix[i] == '-' ? a - b : postfix[i] == '*' ? a * b : a / b);
}
}
return pop();
}
int main() {
char postfix[MAX];
printf("Enter a postfix expression: ");
scanf("%s", postfix);
int result = evalPostfix(postfix);
if (result != -1) printf("Result: %d\n", result);
return 0;
}

You might also like