C program to remove brackets from an algebraic expression

Removing brackets from an algebraic expression.

In this article we will learn how to code a C program to remove brackets from an algebraic expression. We will be using a while loop that will iterate each character till the end of the string , and store the string in new character array . If brackets are found then we will skip them and as the iteration ends we will get an algebraic expression without brackets. 

C program to remove brackets from an algebraic expression

Algorithm:

  • Initialize the variables.
  • Accept the input.
  • Initialize a while loop and terminate it at the end of string. 
  • Iterate each character of the string through that loop.
  • Store these characters into an new array skip brackets , if found.
  •  Print result.

C programming code to remove brackets from an algebraic expression.

Competitive Coding Techniques
#include  <stdio.h>
int main()
{
    //Initializing variables.
    char str[100], str_no_spc[100];
    int i=0, j=0 ;
    
    //Accepting inputs.
    printf("Enter the string:\n");
    gets(str);
    
    //Iterating each character of string.
    while(str[i] != '\0')
    {
        if(str[i] != '(' &&  str[i] != ')')//Excluding brackets.
        {
            str_no_spc[j++] = str[i];
        }
        i++;
    }
    str_no_spc[j] = '\0';
    
    //Printing result.
    printf("The string after removing all the spaces is:\n%s", str_no_spc);
    return 0;
}
Output:
Enter the string:
(p-(r+e)-p)
Enter the string:
(p-(r+e)-p)

One comment on “C program to remove brackets from an algebraic expression”


  • kandukuri

    #include
    #include
    void main()
    {
    int i, j;
    char str[100];
    printf(“enter the string you want:”);
    gets(str);
    for (i = 0; str[i] != 0; i++)
    {
    while (str[i] == ‘(‘ || str[i] == ‘)’)
    {
    for (j = i; str[j] != 0; j++)
    {
    str[j] = str[j + 1];
    }
    str[j] = ‘\0’;
    }
    }
    printf(“the string is new one is %s”, str);
    }