C Online Compiler
Example: Remove Brackets from Algebraic Expression in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Remove Brackets from Algebraic Expression #include <stdio.h> #include <string.h> // Required for strlen, though not strictly used in this solution. // Function to remove brackets from a string // Takes the source string and a destination buffer void removeBrackets(char *source, char *destination) { int i = 0; // Index for source string int j = 0; // Index for destination string // Iterate through the source string until the null terminator is found while (source[i] != '\0') { // Check if the current character is NOT any type of bracket if (source[i] != '(' && source[i] != ')' && source[i] != '[' && source[i] != ']' && source[i] != '{' && source[i] != '}') { // If it's not a bracket, copy it to the destination string destination[j] = source[i]; j++; // Move to the next position in the destination string } i++; // Always move to the next character in the source string } // Null-terminate the destination string to make it a valid C string destination[j] = '\0'; } int main() { // Define an example algebraic expression with various brackets char expression1[] = "a+(b-[c*d])/{e-f}"; char result1[100]; // Buffer to store the result, ensuring enough space // Call the function to remove brackets removeBrackets(expression1, result1); // Print the original and modified expressions printf("Original expression: %s\n", expression1); printf("Expression without brackets: %s\n\n", result1); char expression2[] = "((x+y)*z)+{p-q}"; char result2[100]; removeBrackets(expression2, result2); printf("Original expression: %s\n", expression2); printf("Expression without brackets: %s\n\n", result2); char expression3[] = "simple+expression*no-brackets"; char result3[100]; removeBrackets(expression3, result3); printf("Original expression: %s\n", expression3); printf("Expression without brackets: %s\n", result3); return 0; }
Output
Clear
ADVERTISEMENTS