Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C program to remove the brackets from a given input.
In C programming, removing brackets from mathematical expressions is a common string manipulation task. This involves processing the expression to eliminate parentheses while maintaining the correct mathematical meaning, especially handling sign changes when brackets are preceded by a minus sign.
Syntax
char* removeBrackets(char expression[]);
Algorithm
The algorithm to remove brackets follows these steps −
- Step 1: Read the input expression string
- Step 2: Traverse each character in the string
- Step 3: Copy non-bracket characters to result string
- Step 4: When encountering '(' preceded by '-', flip signs inside brackets
- Step 5: Skip bracket characters themselves
Example: Simple Bracket Removal
This example demonstrates basic bracket removal from a mathematical expression −
#include <stdio.h>
#include <string.h>
void removeBrackets(char input[], char output[]) {
int i = 0, j = 0;
int signFlip = 0;
while (input[i] != '\0') {
if (input[i] == '(') {
// Check if preceded by minus sign
if (i > 0 && input[i-1] == '-') {
signFlip = 1;
j--; // Remove the minus sign before bracket
}
}
else if (input[i] == ')') {
signFlip = 0;
}
else if (signFlip && (input[i] == '+' || input[i] == '-')) {
// Flip the sign inside brackets preceded by minus
output[j++] = (input[i] == '+') ? '-' : '+';
}
else {
output[j++] = input[i];
}
i++;
}
output[j] = '\0';
}
int main() {
char expression[100] = "(x+y)-(z-q)";
char result[100];
printf("Original expression: %s<br>", expression);
removeBrackets(expression, result);
printf("After removing brackets: %s<br>", result);
return 0;
}
Original expression: (x+y)-(z-q) After removing brackets: x+y-z+q
Example: Complex Expression
This example handles more complex expressions with multiple brackets −
#include <stdio.h>
int main() {
char input[100], output[100];
int i = 0, j = 0, signFlip = 0;
printf("Enter mathematical expression: ");
scanf("%s", input);
while (input[i] != '\0') {
if (input[i] == '(') {
if (i > 0 && input[i-1] == '-') {
signFlip = 1;
j--; // Remove minus before opening bracket
}
}
else if (input[i] == ')') {
signFlip = 0;
}
else if (signFlip && (input[i] == '+' || input[i] == '-')) {
output[j++] = (input[i] == '+') ? '-' : '+';
}
else {
output[j++] = input[i];
}
i++;
}
output[j] = '\0';
printf("Result: %s<br>", output);
return 0;
}
Key Points
- Handle sign changes when brackets are preceded by minus operator
- Simple addition brackets can be removed directly
- Maintain proper string null termination
- Use array indexing for efficient character processing
Conclusion
Removing brackets from expressions in C requires careful handling of sign changes, especially when brackets follow a minus operator. The key is to flip signs inside such brackets while preserving the mathematical meaning of the expression.
