i wrote the following program to convert postfix to infix expression…
/postfix to infix.working/
#include<stdio.h>
#include<string.h>
#include<ctype.h>
struct s
{
char arr[40][50];
int top;
};
void post_to_in(char input[50]);
void push(char *str);
char* pop();
void init();
struct s stack;
int main(void)
{
char input[50];
printf("enter the input string\n");
scanf("%s",input);
init();
post_to_in(input);
/* printf("the infix expression is \n");
printf("%s",output);*/
return 0;
}
void post_to_in(char input[50])
{
int len=0;
int i;
char buf[2];
char *item1,*item2;
for(i=0;input[i]!='\0';i++)
{
;
}
len = i;
/*abc, len =3 */
for(i=0;i<len;i++)
{
if(isalnum(input[i]))
{
sprintf(buf,"%c",input[i]);
push(buf);
continue;
}
/*if control here, implies input[i] is an operator*/
item1 = pop();
item2 = pop();
sprintf(buf,"%c",input[i]);
strcat(item2,buf);
strcat(item2,item1);
push(item2);
}
item1 = pop();
printf("the infix expression is \n");
printf("%s",item1);
}
void push(char *str)
{
stack.top++;
strcpy(stack.arr[stack.top],str);
}
char* pop()
{
if(stack.top==-1)
{
printf("this shouldnt be happening\n");
return "";
}
return stack.arr[stack.top--];
}
void init()
{
stack.top =-1;
}
if i were to add the line strcpy(stack.arr[stack.top],""); just before strcpy(stack.arr[stack.top],str); in the push function, this program doesnt work.
I realize that the addition of the new line isnt required, but its addition shouldnt affect the program either, but it does affect the program in this case. why is that?
does strcpy() not allow the same string to be copied into twice ?