BCSL305 Lab Programs 1 To 5
BCSL305 Lab Programs 1 To 5
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct
{
char *day;
int date;
char *activity;
} calendar;
calendar* create()
{
calendar *week;
week=(calendar*)calloc(7,sizeof(calendar));
return week;
}
calendar *week;
while(1)
{
printf("1.Create 2.Read 3.Display 4.Exit choice:");
scanf("%d",&choice);
switch(choice)
{
case 1: week = create();
if(week!=NULL)
printf("Calander is successfully created\n");
break;
case 2: read(week);
break;
case 3: display(week);
break;
case 4: return 0;
default: printf("Invalid choice\n");
}
}
}
Tuesday 12 Assignment
Wednesday 13 Projects
Thursday 14 Coding
Friday 15 Dance
Saturday 16 Music
Sunday 17 Relax
1.Create 2.Read 3.Display 4.Exit choice:4
#include<stdio.h>
char str[100],pat[100],rep[100],ans[100];
void read()
{
printf("enter the string: ");
gets(str);
printf(" \n enter the patter string: ");
flushall();
gets(pat);
printf("\n enter the replacement string: ");
flushall();
gets(rep);
}
void pat_match()
{
int i,j,c,m,k;
int flag=0;
i=m=c=j=0;
while(str[c]!='\0')
{
if(str[m]==pat[i])//Pattern matching
{
i++;
m++;
if(pat[i]=='\0')
{
printf("\n pat:%s is found at position %d",pat,c);
for(k=0;rep[k]!='\0';k++,j++)
ans[j]=rep[k];
i=0;
c=m;
flag=1;
}
}
else//pattern mismatch
{
ans[j]=str[c];
j++;
c++;
m=c;
i=0;
}
}
ans[j]='\0';
if(flag==0)
printf("\n PAT:%s is not found in STR:%s",pat,str);
else
printf("\n The resulting string is: %s",ans);
}
void main()
{
clrscr();
read();
pat_match();
getch();
}
OUTPUT:-
enter the string: hi rns hi
enter the pattern string: hi
enter the replacement string: hello
pat:hi is found at position 0
pat:hi is found at position 7
The resulting string is: hello rns hello
#include<stdio.h>
#include<math.h>
#define max 5
int stack_list[max],top=-1;
void push(int m)
{
if(top==max-1)
printf("\n Stack overflow");
else
{
top++;
stack_list[top]=m;
}
}
int pop()
{
if(top==-1)
{
printf("\n Stack underflow");
return -1;
}
else
return stack_list[top--];
}
void display()
{
int i;
if(top==-1)
printf("\n Stack is empty");
else
{
printf("\n The elements are\n");
for(i=top;i>=0;i--)
printf("%d\n",stack_list[i]);
}
}
void palindrome()
{
int n,num,rem,i;
printf("\n Enter n");
scanf("%d",&n);
top=-1;
num=n;
while(num!=0)
{
rem=num%10;
push(rem);
num=num/10;
}
num=0;
for(i=0;top!=-1;i++)
num=pop()*pow(10,i)+num;
if(n==num)
printf("\n It is a palindrome");
else
printf("\n It is not a palindrome");
}
int main()
{
int c,m;
while(1)
{ printf("\n Enter 1-push\n2-pop\n3-display\n4-palindrome");
scanf("%d",&c);
switch(c)
{
case 1: printf("\n Enter an element\t");
scanf("%d",&m);
push(m);
break;
case 2:m=pop();
if(m!=-1)
printf("\n The popped elementis %d",m);
break;
case 3: display();
break;
case 4: palindrome();
break;
default:return 0;
}
}
}
Stack overflow
Enter your choice
1-push
2-pop
3-display
4-palindrome
3
2-pop
3-display
4-palindrome
2
Stack underflow
Enter n
123
It is not a palindrome
Enter your choice
1-push
2-pop
3-display
4-palindrome
4
Enter n
121
It is a palindrome
Enter your choice
1-push
2-pop
3-display
4-palindrome
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
char stk[100];
int tos = -1;
void push(char opr)
{
stk[++tos] = opr;
}
char pop()
{
return(stk[tos--]);
}
int preced(char opr)
{
if(opr=='^'||opr=='%') return(4);
if(opr=='*'||opr=='/') return(3);
if(opr=='+'||opr=='-') return(2);
if(opr=='('||opr=='#') return(1);
}
void main()
{
char infix[20],postfix[20];
int i,j=0;
printf("\nEnter valid INFIX expression\n");
scanf("%s",infix);
push('#');
for(i=0; infix[i]!='\0'; i++)
{
if(infix[i]=='(')
push('(');
else if(isalnum(infix[i]))
postfix[j++] = infix[i];
else if(infix[i]==')')
{
while(stk[tos] != '(')
postfix[j++] = pop();
pop();
}
else
{
while(preced(stk[tos]) >= preced(infix[i]))
postfix[j++] = pop();
push(infix[i]);
}
}
while(stk[tos] != '#')
postfix[j++] = pop();
postfix[j]='\0';
printf("\n INFIX EXPRESSION = %s",infix);
printf("\n POSTFIX EXPRESSION = %s",postfix);
getch();
}
OUTPUT:-
5 Design, Develop and Implement a program in C for the following stack operations
a. Evaluation of suffix expression with single digit operands and operators:+,-
,*,/,%,^.
b. Solving tower of Hanoi problem with n disks.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
int stk[25],tos=-1;
void push(int item)
{
stk[++tos]=item;
}
int pop()
{
return (stk[tos--]);
}
int main()
{
char post[25],sym;
int op1,op2,i;
printf("Enter the postfix expression:\n");
scanf("%s",post);
for(i=0;i<strlen(post);i++)
{
sym=post[i];
switch(sym)
{
case '+':op2=pop();
op1=pop();
push(op1+op2);
break;
case '-':op2=pop();
op1=pop();
push(op1-op2);
break;
case '*':op2=pop();
op1=pop();
push(op1*op2);
break;
case '/':op2=pop();
op1=pop();
push(op1/op2);
break;
default:push(sym-'0');
break;
}
}
printf("The result if %d\n",pop());
}
Output1:
Enter the postfix expression:
6523+8*+3+*
The result if 288
Output2:
Enter the postfix expression:
93*1-2+4*5-
The result if 107
#include<stdio.h>
void tower(int num,char src,char tmp,char dest)
{
if(num==1)
{
printf("Move disk 1 from peg %c to peg %c\n",src,dest);
return;
}
tower(num-1,src,dest,tmp);
printf("Move disk %d from peg %c to peg %c\n",num,src,dest);
tower(num-1,tmp,src,dest);
}
int main()
{
int num;
printf("Enter number of disks\n");
scanf("%d",&num);
tower(num,'A','B','C');
}
Output1:
Enter number of disks
3
Move disk 1 from peg A to peg C
Move disk 2 from peg A to peg B
Move disk 1 from peg C to peg B
Move disk 3 from peg A to peg C
Move disk 1 from peg B to peg A
Move disk 2 from peg B to peg C
Move disk 1 from peg A to peg C