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. 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.  #include  <stdio.h>
int main()
{
//Initializing variables.
char str, str_no_spc;
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)

3 comments on “C program to remove brackets from an algebraic expression”

• Geetha

#python code
n=input()
A=[]
for i in n:
if i=='(‘ or i==’)’:
pass
else:
A.append(i)
print(*A,sep=”) 0
• Prathamesh

#IN Python:
string=input()
string2=’ ‘
for i in string:
if(ord(i) == 40 or ord(i) ==41) :
pass
else:
string2+=I
print(string2) 0
• kandukuri

#include
#include
void main()
{
int i, j;
char str;
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);
} 4