Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-operator symbol like '&' or '%'.
Forgetting to include '/' as an operator.
✗ Incorrect
The division operator '/' is a common operator in infix expressions and must be checked.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' or '-' which have lower precedence.
Using '^' which is not handled here.
✗ Incorrect
Division '/' has the same precedence as multiplication '*', so it should return 2.
3fill in blank
hardFix 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'
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.
✗ Incorrect
We must increment the value pointed by top, so ++*top is correct.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'top' without dereferencing causes errors.
Using '&top' is incorrect as it is address of pointer.
✗ Incorrect
(*top)-- correctly returns the top element and then decrements the top index.
5fill in blank
hardFill 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'
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.
✗ Incorrect
When encountering ')', pop until '(' is found (blank 1 = '(').The operator to push is c (blank 3 = c).
