PRAJWALDSL

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

#include <stdio.

h>

#include <stdlib.h>

#include <string.h>

struct Day {

char *dayName;

int date;

char *activityDescription;

};

void create(struct Day *calendar) {

char *dayNames[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday",


"Sunday"};

for (int i = 0; i < 7; ++i) {

calendar[i].dayName = strdup(dayNames[i]);

printf("Enter date for %s: ", calendar[i].dayName);

scanf("%d", &calendar[i].date);

printf("Enter activity for %s: ", calendar[i].dayName);

while (getchar() != '\n');

size_t bufferSize = 256;

calendar[i].activityDescription = (char *)malloc(bufferSize * sizeof(char));

getline(&calendar[i].activityDescription, &bufferSize, stdin);

void read(struct Day *calendar) {

for (int i = 0; i < 7; ++i) {

printf("Enter date for %s: ", calendar[i].dayName);

scanf("%d", &calendar[i].date);

printf("Enter activity for %s: ", calendar[i].dayName);

while (getchar() != '\n');

size_t bufferSize = 256;

calendar[i].activityDescription = (char *)malloc(bufferSize * sizeof(char));

getline(&calendar[i].activityDescription, &bufferSize, stdin);


}

void display(struct Day *calendar) {

printf("%-10s %-10s %-20s\n", "Day", "Date", "Activity");

for (int i = 0; i < 7; ++i) {

printf("%-10s %-10d %-20s\n", calendar[i].dayName, calendar[i].date,


calendar[i].activityDescription);

int main() {

struct Day *calendar = (struct Day *)malloc(7 * sizeof(struct Day));

if (calendar == NULL) {

fprintf(stderr, "Memory allocation failed\n");

return 1;

create(calendar);

display(calendar);

for (int i = 0; i < 7; ++i) {

free(calendar[i].dayName);

free(calendar[i].activityDescription);

free(calendar);

return 0;

}
2)

#include<stdio.h>

char str[100], pat[50], rep[50], ans[100];

int i, j, c, m, k, flag=0;

void stringmatch()

i = m = c = j = 0;

while (str[c] != '\0')

if(str[m] == pat[i])

i++; m++;

if(pat[i] == '\0')

flag = 1;

for(k = 0; rep[k] != '\0'; k++, j++)

ans[j] = rep[k];

i = 0;

c = m;

else

ans[j] = str[c];

j++;

c++;

m = c;

i = 0;

ans[j] = '\0';
}

void main()

printf("\nEnter a main string \n");

gets(str);

printf("\nEnter a pattern string \n");

gets(pat);

printf("\nEnter a replace string \n");

gets(rep);

stringmatch();

if(flag == 1)

printf("\nThe resultant string is\n %s" , ans);

else

printf("\nPattern string NOT found\n");

}
3)

#include <stdio.h>

#include <ctype.h>

#define SIZE 50

char s[SIZE];

int top = -1

push(char elem)

s[++top] = elem;

char pop()

return (s[top--]);

int pr(char elem)

switch (elem)

case '#':

return 0;

case '(':

return 1;

case '+':

case '-':

return 2;

case '*':

case '/':

case '%':

return 3;

case '^':

return 4;
}

void main() {

char infx[50], pofx[50], ch, elem;

int i = 0, k = 0;

printf("\n\nRead the Infix Expression ? ");

scanf("%s", infx);

push('#');

while ((ch = infx[i++]) != '\0')

if (ch == '(')

push(ch);

else if (isalnum(ch))

pofx[k++] = ch; else

if (ch == ')')

while (s[top] != '(')

pofx[k++] = pop();

elem = pop();

}else

while (pr(s[top]) >= pr(ch))

pofx[k++] = pop();

push(ch);

while (s[top] != '#')

pofx[k++] = pop();

pofx[k] = '\0';

printf("\n\nGiven Infix Expn: %s Postfix Expn: %s\n", infx, pofx);

}
4)

#include <stdio.h>

#include <ctype.h>

#define SIZE 50

char s[SIZE];

int top = -1;

push(char elem)

s[++top] = elem;

char pop()

return (s[top--]);

int pr(char elem)

switch (elem)

case '#':

return 0;

case '(':

return 1;

case '+':

case '-':

return 2;

case '*':

case '/':

case '%':

return 3;

case '^':

return 4;
}

void main() {

char infx[50], pofx[50], ch, elem;

int i = 0, k = 0;

printf("\n\nRead the Infix Expression ? ");

scanf("%s", infx);

push('#');

while ((ch = infx[i++]) != '\0')

if (ch == '(')

push(ch);

else if (isalnum(ch))

pofx[k++] = ch; else

if (ch == ')')

while (s[top] != '(')

pofx[k++] = pop();

elem = pop();

else

while (pr(s[top]) >= pr(ch))

pofx[k++] = pop();

push(ch);

while (s[top] != '#')pofx[k++] = pop();

pofx[k] = '\0';

printf("\n\nGiven Infix Expn: %s Postfix Expn: %s\n", infx, pofx);

You might also like