0

For replacing numbers in my variable expression , I wrote a program to get the expression with variables , and for each variable I read the number value from the user and replace it.

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

char *OPRND_Stack = NULL;


int main()
{
    // getting postfix string
    char postfix[40];
    printf("Enter the postfix expression: ");
    scanf("%s",postfix);

    int postfix_length = strlen(postfix);

    printf("Enter the value of: \n");

    
    int arrayIndex = 0; 
    char temp;
    while(postfix[arrayIndex] != '\0')
    {
        if(isalpha(postfix[arrayIndex]))
        {
            printf("%c : ",postfix[arrayIndex]);
            scanf("%c",&temp);
            postfix[arrayIndex] = temp;
        }
        arrayIndex++;
    }
    printf("\nTransformed Expression : %s",postfix);
}

The output is

Enter the postfix expression: ab+c+
Enter the value of:
a : b : 3
c :
Transformed Expression :
3+
+

But i should get like this,

ab+c+

a : 5
b : 3
c : 2

Transformed expression : 53+2+

What is the mistake i had done or what might be the cause for skipping my input and assigning new line as its value.

  • In many contexts, is is important to use `" %c"` to read single characters with `scanf()`. That blank is all-important. There are only three conversion specifiers for the `scanf()` family of functions that don't skip white space, and they are `%c`, `%[…]` (scan sets), and `%n`. Newlines count as white space. The blank makes `scanf()` skip optional white space, such as the newline after the first number you entered. – Jonathan Leffler Apr 24 '23 at 13:44

0 Answers0