Compiler Design
Program to search a character from a given string.
#include<iostream.h>
#include<conio.h>
void main()
{
char a[20];
char b;
int l,i,j,k,flag=0;
cout<<"Enter the length of string:";
cin>>l;
cout<<"Enter the string:";
for(i=0;i<l;i++)
{
cin>>a[i];
}
cout<<"\nthe entered string is:";
for(j=0;j<l;j++)
{
cout<<a[j];
}
cout<<"\nEnter the character you want to search:";
cin>>b;
for(k=0;k<l;k++)
{
if(a[k]==b)
{
flag=1;
cout<<"index of "<<b<<" is:"<<k+1;
break;
}
}
if(flag==0)
{
cout<<"the character does not exist:\n";
}
getch();
}
Vikas Verma (0829cs071119) Page 1
Compiler Design
OUTPUT:
Vikas Verma (0829cs071119) Page 2
Compiler Design
Program to convert given infix expression to postfix.
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
#define MAX 50
struct node
{
char target[MAX] ;
char stack[MAX] ;
char *s, *t ;
int top ;
};
void initinfix ( struct node * ) ;
void setexpr ( struct node *, char * ) ;
void push ( struct node *, char ) ;
char pop ( struct node * ) ;
void convert ( struct node * ) ;
int priority ( char ) ;
void show ( struct node ) ;
void main( )
{
struct node p ;
char expr[MAX] ;
initinfix ( &p ) ;
clrscr( ) ;
printf ( "\nEnter an expression in infix form: " ) ;
gets ( expr ) ;
setexpr ( &p, expr ) ;
convert ( &p ) ;
printf ( "\nThe postfix expression is: " ) ;
show ( p ) ;
Vikas Verma (0829cs071119) Page 3
Compiler Design
getch( ) ;
}
/* initializes structure elements */
void initinfix ( struct node *p )
{
p -> top = -1 ;
strcpy ( p -> target, "" ) ;
strcpy ( p -> stack, "" ) ;
p -> t = p -> target ;
p -> s = "" ;
}
/* sets s to point to given expr. */
void setexpr ( struct node *p, char *str )
{
p -> s = str ;
}
/* adds an operator to the stack */
void push ( struct node *p, char c )
{
if ( p -> top == MAX )
printf ( "\nStack is full.\n" ) ;
else
{
p -> top++ ;
p -> stack[p -> top] = c ;
}
}
/* pops an operator from the stack */
char pop ( struct node *p )
{
if ( p -> top == -1 )
{
return -1 ;
}
else
Vikas Verma (0829cs071119) Page 4
Compiler Design
{
char item = p -> stack[p -> top] ;
p -> top-- ;
return item ;
}
}
/* converts the given expr. from infix to postfix form */
void convert ( struct node *p )
{
char opr ;
while ( *( p -> s ) )
{
if ( *( p -> s ) == ' ' || *( p -> s ) == '\t' )
{
p -> s++ ;
continue ;
}
if ( isdigit ( *( p -> s ) ) || isalpha ( *( p -> s ) ) )
{
while ( isdigit ( *( p -> s ) ) || isalpha ( *( p -> s ) ) )
{
*( p -> t ) = *( p -> s ) ;
p -> s++ ;
p -> t++ ;
}
}
if ( *( p -> s ) == '*' || *( p -> s ) == '+' || *( p -> s ) == '/' || *( p -> s ) == '%' || *( p -> s ) == '-' || *(
p -> s ) == '$' )
{
if ( p -> top != -1 )
{
opr = pop ( p ) ;
while ( priority ( opr ) >= priority ( *( p -> s ) ) )
{
*( p -> t ) = opr ;
p -> t++ ;
opr = pop ( p ) ;
}
Vikas Verma (0829cs071119) Page 5
Compiler Design
push ( p, opr ) ;
push ( p, *( p -> s ) ) ;
}
else
push ( p, *( p -> s ) ) ;
p -> s++ ;
}
if ( *( p -> s ) == ')' )
{
opr = pop ( p ) ;
while ( ( opr ) != '(' )
{
*( p -> t ) = opr ;
p -> t++ ;
opr = pop ( p ) ;
}
p -> s++ ;
}
}
while ( p -> top != -1 )
{
char opr = pop ( p ) ;
*( p -> t ) = opr ;
p -> t++ ;
}
*( p -> t ) = '\0' ;
}
/* returns the priority of an operator */
int priority ( char c )
{
switch(c)
{
case '$':
return 3 ;
break;
case '*':
Vikas Verma (0829cs071119) Page 6
Compiler Design
return 2 ;
break;
case '/':
return 2 ;
break;
case '%':
return 2 ;
break;
case '+':
return 1 ;
break;
case '-':
return 1 ;
break;
}
}
/* displays the postfix form of given expr. */
void show ( struct node p )
{
printf ( " %s", p.target ) ;
}
Vikas Verma (0829cs071119) Page 7
Compiler Design
OUTPUT:
Vikas Verma (0829cs071119) Page 8
Compiler Design
Program to evaluate a postfix expression.
# include<stdio.h>
# include<conio.h>
# include<ctype.h>
/*program to evaluate the given postfix expression*/
typedef struct
{
int a[100];
int top;
}STACK;
void push(STACK *s,int x)
{
if(s->top==99)
printf("STACK OVERFLOW\n");
else
s->a[++s->top]=x;
}
int pop(STACK *s)
{
int x;
if(s->top<0)
printf("STACK UNDERFLOW\n");
else
{
x=s->a[s->top--];
return x;
}
}
int operation(int p1,int p2,char op)
{
switch(op)
{
case '+':return p1+p2;
case '*':return p1*p2;
case '-':return p1-p2;
case '/':return p1/p2;
}
}
int evaluate(char pos[])
{
STACK s1;
int p1,p2,result,i;
Vikas Verma (0829cs071119) Page 9
Compiler Design
s1.top=-1;
for(i=0;pos[i]!='\0';i++)
if(isdigit(pos[i]))
push(&s1,pos[i]-'0');/*use to find the integer value of it*/
else
{
p2=pop(&s1);
p1=pop(&s1);
result=operation(p1,p2,pos[i]);
push(&s1,result);
}/*end of for loop*/
return pop(&s1);
}
void main()
{
char postfix[100];
clrscr();
printf("Please Enter the VALID POSTFIX string\n\nOperands are SINGLE DIGIT\n\n");
gets(postfix);
printf("The Result is==>%d",evaluate(postfix));
getch();
}
Vikas Verma (0829cs071119) Page 10
Compiler Design
OUTPUT:
Vikas Verma (0829cs071119) Page 11
Compiler Design
Program for passing arguments using Call By Value
#include<stdio.h>
#include<conio.h>
void interchange(int ,int);
void main()
int x,y;
printf("enter two numbers:\n");
scanf("%d %d",&x,&y);
printf("\nBefore calling interchange() function:\n");
printf("x=%d \t y=%d",x,y);
interchange(x,y);
printf("\nOriginal Values inside main:\n x=%d \t y=%d",x,y);
void interchange(int x1,int y1)
int z1;
z1=x1;
x1=y1;
y1=z1;
printf("\nValues inside interchange() function:\n x1=%d \t y1=%d",x1,y1);
Vikas Verma (0829cs071119) Page 12
Compiler Design
OUTPUT:
Vikas Verma (0829cs071119) Page 13
Compiler Design
Program for passing arguments using Call By Reference
#include<iostream.h>
#include<conio.h>
void swapref(int &, int &);
void main()
int a, b;
cout << "Enter two numbers =\n" ;
cin >> a >> b ;
cout << "Before calling swapref() function.";
cout << "\na = "<< a << "\tb = "<< b;
swapref(a, b);
cout << "\nAfter calling swapref() function.";
cout << "\na = "<< a << "\tb = " << b;
void swapref(int &aa, int &bb)
int temp;
temp = aa;
aa = bb;
bb = temp;
Vikas Verma (0829cs071119) Page 14
Compiler Design
OUTPUT:
Vikas Verma (0829cs071119) Page 15
Compiler Design
Program for passing arguments using Call By Address
#include<iostream.h>
#include<conio.h>
void swapadd (int *, int *);
void main ()
{
int a, b ;
cout << "\nEnter two numbers = \n" ;
cin >> a >> b;
cout << "Before calling swapadd() function.";
cout << "\na = " << a;
cout << "\nb = " << b;
swapadd (&a, &b ) ;
cout << "\nAfter calling swapadd() function.";
cout << "\na = " << a;
cout << "\nb = " << b ;
}
void swapadd(int *aa, int *bb)
{
int temp;
temp = (*aa);
(*aa) = (*bb);
(*bb) = temp;
}
Vikas Verma (0829cs071119) Page 16
Compiler Design
OUTPUT:
Vikas Verma (0829cs071119) Page 17