-3
Input:
3
(a+(b*c))
((a+b)*(z+x))
((a+t)*((b+(a+c))^(c+d)))

Output:
abc*+
ab+zx+*
at+bac++cd+^*

There are brackets in all the inputs, so precedence order of operators need not be checked. The following is my solution:

#include <stdio.h>
#include <string.h>

int main (void)
{
  unsigned short t;
  int len ,top = -1, i;
  char str[400], newStr[400], stack[400];
  char* p_newStr = newStr;
  scanf("%hu", &t);
  while (t--)
  {
    if (scanf("%s", str) <= 400)
    {
      len = strlen(str);
      i = 0;
      while(i < len)
      {
        if (str[i] >= 97 && str[i] <= 122)
        {
          p_newStr = p_newStr + str[i];
        }
        else if (str[i] == '(' || str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/' || str[i] == '^')
        {
          top++;
          stack[top] = str[i];
        }
        else if (str[i] == ')')
        {
          while(stack[top] != '(')
          {
            p_newStr = p_newStr + stack[top];
            top--;
          }
          top--; //the '(' is discarded from the stack.
        }
        i++;
      }
      printf ("%s\n", newStr);
    }
  }
  return 0;
}

I do not have much experience with handling strings in C or C++ using character arrays or library classes. When I run it, I get un recognizable symbols instead of the correct postfix notation.

tripleee
  • 175,061
  • 34
  • 275
  • 318
aste123
  • 1,223
  • 4
  • 20
  • 40

2 Answers2

1

The problem is that you process p_newStr without initializint it, and only performing pointer arithmetic on it. I guess, that you wanted to see it as a string, adding chars to it.

So first initialisze it:

    char* p_newStr = newStr; // It was unitinitalised, pointing at random location

Then note that p_newStr = p_newStr + str[i] means adding the value of the char str[i], converted to int, to the pointer p_newStr. That's pointer arithmetic. Replace this statement with:

    *p_newStr++ = str[i];   // instead of p_newStr = ... 

and there is another one taking a char from the stack[]:

    *p_newStr++ = stack[top];   // instead of p_newStr = ... 

Finally, put an ending null terminator to the c string you've built, just before your printf():

    *p_newStr++ = 0;

Once these corrections made, the code produces exactly the expected results.

Christophe
  • 68,716
  • 7
  • 72
  • 138
  • since newStr was created with `char newStr[400]`. Can I just use `newStr` as the pointer to the string instead of the `p_newStr`? – aste123 Nov 22 '14 at 23:20
  • no, because `newStr[]` is an array. Writing `newstr` just converts it to a pointer. If you're not familiar with pointers you could however use newstr[j] where j is an index starting at 0 and incremented after you add a new char – Christophe Nov 22 '14 at 23:28
  • What is the difference between `newStr` and `p_newStr`. Since newStr[] is an array, newStr should point to the address of newStr[0]. Is the not same as p_newStr? There is another problem with my solution. I need to handle more than one strings for example input is `3 (a+(b*c)) ((a+b)*(z+x)) ((a+t)*((b+(a+c))^(c+d)))`. In this case, the logic of my code still works but I need to be able to **reinitialize** the character arrays so that they can store new strings instead of appending the new characters after the already added NULL or 0 character. How do I do this? – aste123 Nov 22 '14 at 23:33
  • 1
    p_newStr is just a pointer that could point anywhere. The simailarity of its name is not sufficient: you have to initialisz if explicitely. If you want to reinitialize, just reassign `p_newStr=newStr`: the new chars you add will overwrite the old ones. The ending 0 will cut the string in case the old string was longer. – Christophe Nov 22 '14 at 23:39
  • 1
    Thank you! I get it. So that means we store address of `newStr[0]` in `p_newStr` and increment it as required. We cannot increment `newStr` as it is like a constant pointing to `newStr[0]`. That also explains how the reinitialization works. – aste123 Nov 22 '14 at 23:44
0
p_newStr = p_newStr + str[i];

Should be:

*p_newStr++ = str[i];

The way you currently have it, you are never actually modifying the new string. You make this mistake in two places.

JS1
  • 4,745
  • 1
  • 14
  • 19