Advertisement
Arnab_Manna

prefix2Infix

Dec 22nd, 2022
993
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.90 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<ctype.h>
  3. #include<string.h>
  4. #include<stdlib.h>
  5.  
  6. char S[50][100],res[100];
  7. int top=-1;
  8.  
  9. void push(char *c)
  10. {
  11.     strcpy(S[++top],c);
  12. }
  13.  
  14. void pop()
  15. {
  16.     strcpy(res,S[top--]);
  17. }
  18. void clear(char *c)
  19. {
  20.     int i,j=strlen(c);
  21.     for(i=0; i<j;i++)
  22.     {
  23.         c[i]='\0';
  24.     }
  25. }
  26.  
  27. int main()
  28. {
  29.     char exp[100],temp[100],b1[]="(",b2[]=")",op1[100],op2[100];
  30.     char ptr[100];
  31.     clear(ptr);
  32.     int i;
  33.     puts("enter sting 1:");
  34.     gets(exp);
  35.     strrev(exp);
  36.    
  37.     int j=strlen(exp);
  38.    
  39.     for(i=0; i<j;i++)
  40.     {
  41.         if(isalnum(exp[i]))
  42.         {
  43.             temp[0]=exp[i];
  44.             push(temp);
  45.         }
  46.         else
  47.         {
  48.             pop();strcpy(op2,res);
  49.             pop();strcpy(op1,res);
  50.             temp[0]=exp[i];
  51.             strcat(ptr,b2);
  52.             strcat(ptr,op1);
  53.             strcat(ptr,temp);
  54.             strcat(ptr,op2);
  55.             strcat(ptr,b1);
  56.            
  57.             push(ptr);
  58.             clear(ptr);
  59.         }
  60.     }
  61.     pop();
  62.     puts("the converted infix is :");
  63.     strrev(res);
  64.     puts(res);
  65.  
  66.  
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement