C program to convert infix to prefix notation

Posted: April 2, 2012 in Lab 7
/*     Infix to prefix notation
written by- Shivam Rana 
Date- 2/4/12          
Users are requested to use a bracket wherever necessary while giving input otherwise
it might give wrong answer     */
#include<stdio.h>
#include<string.h>
int main()
{
int i,j,l,m,k=0;
char a[400],b[400],reva[400],ans[400];
m=-1;
scanf("%s",&a);
l=strlen(a);
	for(i=0;i<l;i++)
	{
		if(a[l-1-i]=='(')
			reva[i]=')';
		else
			if(a[l-1-i]==')')
			reva[i]='(';
		else
		reva[i]=a[l-1-i];
	}
for(j=0;j<l;j++)
{
if(reva[j]=='(')
continue;
else if(reva[j]>64 && reva[j]<91 ||reva[j]>96 && reva[j]<123)
			ans[k++]=reva[j];
else if(reva[j]==')')
{
			ans[k++]=b[m];
m--;
}
else
{
m++;
b[m]=reva[j];
}
}
	for(i=(k-1);i>=0;i--)
		printf("%c",ans[i]);
	printf("\n");

return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s