Bird
0
0
DSA Cprogramming~10 mins

Infix to Postfix Conversion Using Stack in DSA C - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if a character is an operator.

DSA C
int isOperator(char c) {
    return (c == '+' || c == '-' || c == '*' || c == [1]);
}
Drag options to blanks, or click blank then click option'
A/
B%
C^
D&
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-operator symbol like '&' or '%'.
Forgetting to include '/' as an operator.
2fill in blank
medium

Complete the code to return precedence of operators.

DSA C
int precedence(char op) {
    if (op == '+' || op == '-')
        return 1;
    if (op == '*' || op == [1])
        return 2;
    return 0;
}
Drag options to blanks, or click blank then click option'
A-
B/
C^
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' or '-' which have lower precedence.
Using '^' which is not handled here.
3fill in blank
hard

Fix the error in the stack push function to correctly add an element.

DSA C
void push(char stack[], int *top, char value) {
    stack[++[1]] = value;
}
Drag options to blanks, or click blank then click option'
A*top
Btop
C&top
D(*top)
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'top' directly increments the pointer, not the value.
Using '&top' is incorrect as it gives address of pointer.
4fill in blank
hard

Fill in the blank to correctly pop an element from the stack.

DSA C
char pop(char stack[], int *top) {
    return stack[[1]--];
}
Drag options to blanks, or click blank then click option'
A*top
Btop
C(*top)
D&top
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'top' without dereferencing causes errors.
Using '&top' is incorrect as it is address of pointer.
5fill in blank
hard

Fill both blanks to correctly convert infix to postfix expression.

DSA C
void infixToPostfix(char* infix, char* postfix) {
    char stack[100];
    int top = -1, i = 0, k = 0;
    while (infix[i] != '\0') {
        char c = infix[i];
        if (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
            postfix[k++] = c;
        } else if (c == '(') {
            stack[++top] = c;
        } else if (c == ')') {
            while (top != -1 && stack[top] != () {
                postfix[k++] = stack[top--];
            }
            top--;
        } else if (isOperator(c)) {
            while (top != -1 && precedence(stack[top]) >= [1]) {
                postfix[k++] = stack[top--];
            }
            stack[++top] = [2];
        }
        i++;
    }
    while (top != -1) {
        postfix[k++] = stack[top--];
    }
    postfix[k] = '\0';
}
Drag options to blanks, or click blank then click option'
A(
Bc
Ctop
Dprecedence(c)
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong character for '(' in popping loop.
Comparing precedence with stack top instead of current operator.
Pushing wrong character onto stack.